[英]Url that is relative to an ASP.NET MVC Area?
I have an Area like this: 我有一个这样的区域:
Areas
MiniBlog
Controllers
themes
MyTheme
Post.cshtml
Views
Blog
Index.cshtml
Inside Index.cshtml I am using the following code: 在Index.cshtml内部,我正在使用以下代码:
@RenderPage("~/Areas/MiniBlog/themes/MyTheme/Post.cshtml", post);
I would rather do something like this: 我宁愿做这样的事情:
@RenderPage(@currentArea + "themes/MyTheme/Post.cshtml", post);
How do we get the path to the current area so we can prevent hard coding it? 我们如何获得到当前区域的路径,从而可以防止对其进行硬编码?
How about writing a helper like this: 如何编写这样的助手:
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);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.