编辑:以下将给出带有控制器的程序集的路径以及带有控制器动作的类的类型名称。也许这些的组合会给你你所追求的,亚伦?
string assemblyPath = Assembly.GetExecutingAssembly().CodeBase;
string typeName = this.GetType().FullName;
例如,它们会产生类似的东西
file:///C:/Projects/TestApp/TestApp.UI/bin/TestApp.UI.DLL
TestApp.UI.Controllers.TestController
假设您以“标准”ASP.NET MVC 方式放置和命名控制器,则上述某种组合可能会为您提供 C# 文件的正确完整路径:
C:/Projects/TestApp/TestApp.UI/Controllers/TestController.cs
或相对路径:
Controllers/TestController.cs
以下将给出控制器动作的路线:
1) string path = Request.Url.AbsolutePath
2) string appPath = Request.ApplicationPath;
string absPath = Request.Url.AbsolutePath;
string path = appPath.Length <= 1 ?
absPath : absPath.Replace(appPath, "");
请求 TestController 的 Index 操作的示例(http://localhost:50027/Test/Index):以上返回
1) /Test/Index
2) /Test/Index
对于基本 url 为http://localhost:50027/blog的网站,例如请求 TestController 的 Index 操作 ( http://localhost:50027/blog/Test/Index ):以上返回
1) /blog/Test/Index
2) /Test/Index