我如何获得控制器的路径?例如,我可以像这样得到HtmlHelper的路径:
private static string GetVirtualPath(HtmlHelper htmlhelper)
{
string virtualPath = null;
TemplateControl tc = htmlhelper.ViewDataContainer as TemplateControl;
if (tc != null)
{
virtualPath = tc.AppRelativeVirtualPath;
}
return virtualPath;
}
Run Code Online (Sandbox Code Playgroud)
编辑:以下将给出具有控制器的程序集的路径以及具有控制器操作的类的类型名称.可能这些组合会给你你所追求的东西,亚伦?
string assemblyPath = Assembly.GetExecutingAssembly().CodeBase;
string typeName = this.GetType().FullName;
Run Code Online (Sandbox Code Playgroud)
例如,他们会产生类似的东西
file:///C:/Projects/TestApp/TestApp.UI/bin/TestApp.UI.DLL
TestApp.UI.Controllers.TestController
Run Code Online (Sandbox Code Playgroud)
如果您以"标准"ASP.NET MVC方式放置和命名控制器,则上述某种组合可能会为您提供C#文件的正确完整路径:
C:/Projects/TestApp/TestApp.UI/Controllers/TestController.cs
Run Code Online (Sandbox Code Playgroud)
或相对路径:
Controllers/TestController.cs
Run Code Online (Sandbox Code Playgroud)
以下将给出控制器操作的路由:
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, "");
Run Code Online (Sandbox Code Playgroud)
TestController的索引操作请求示例(http:// localhost:50027/Test/Index):以上返回
1) /Test/Index
2) /Test/Index
Run Code Online (Sandbox Code Playgroud)
对于在http:// localhost:50027/blog中具有基本URL的网站,请求TestController的索引操作的示例(http:// localhost:50027/blog/Test/Index):以上返回
1) /blog/Test/Index
2) /Test/Index
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
7697 次 |
| 最近记录: |