C# 相对于ASP.NET MVC区域的Url?
我有这样一个领域:C# 相对于ASP.NET MVC区域的Url?,c#,asp.net,asp.net-mvc,razor,C#,Asp.net,Asp.net Mvc,Razor,我有这样一个领域: Areas MiniBlog Controllers themes MyTheme Post.cshtml Views Blog Index.cshtml @RenderPage(@currentArea + "themes/MyTheme/Post.cshtml", post); public stati
Areas
MiniBlog
Controllers
themes
MyTheme
Post.cshtml
Views
Blog
Index.cshtml
@RenderPage(@currentArea + "themes/MyTheme/Post.cshtml", post);
public static class ViewUtility
{
private const string _areasRoot = "~/Areas/";
public static string CurrentAreaRelativePath(string path)
{
var currentArea = HttpContext.Current.Request.RequestContext.RouteData.DataTokens["area"];
return VirtualPathUtility.Combine(_areasRoot, currentArea + "/" + path);
}
}
@RenderPage(ViewUtility.CurrentAreaRelativePath("themes/MyTheme/Post.cshtml"), post);
在Index.cshtml内部,我使用以下代码:
@RenderPage("~/Areas/MiniBlog/themes/MyTheme/Post.cshtml", post);
我宁愿这样做:
Areas
MiniBlog
Controllers
themes
MyTheme
Post.cshtml
Views
Blog
Index.cshtml
@RenderPage(@currentArea + "themes/MyTheme/Post.cshtml", post);
public static class ViewUtility
{
private const string _areasRoot = "~/Areas/";
public static string CurrentAreaRelativePath(string path)
{
var currentArea = HttpContext.Current.Request.RequestContext.RouteData.DataTokens["area"];
return VirtualPathUtility.Combine(_areasRoot, currentArea + "/" + path);
}
}
@RenderPage(ViewUtility.CurrentAreaRelativePath("themes/MyTheme/Post.cshtml"), post);
如何获取当前区域的路径以防止硬编码?如何编写这样的帮助程序:
Areas
MiniBlog
Controllers
themes
MyTheme
Post.cshtml
Views
Blog
Index.cshtml
@RenderPage(@currentArea + "themes/MyTheme/Post.cshtml", post);
public static class ViewUtility
{
private const string _areasRoot = "~/Areas/";
public static string CurrentAreaRelativePath(string path)
{
var currentArea = HttpContext.Current.Request.RequestContext.RouteData.DataTokens["area"];
return VirtualPathUtility.Combine(_areasRoot, currentArea + "/" + path);
}
}
@RenderPage(ViewUtility.CurrentAreaRelativePath("themes/MyTheme/Post.cshtml"), post);
我不确定区域名称是否有内置属性。但是,您可以将AreaRegistration.AreaName返回的常量设置为公共。但我认为常用的方法是在专用文件夹中包含部分视图,这样我们就可以调用RenderPartial(“Post”),如果您使用T4MVC,那么很容易获取URL。t4mvc.codeplex.com/