相对于ASP.NET MVC区域的Url
本文关键字:Url 区域 MVC ASP NET 相对于 | 更新日期: 2023-09-27 17:59:11
我有一个这样的区域:
Areas
MiniBlog
Controllers
themes
MyTheme
Post.cshtml
Views
Blog
Index.cshtml
在Index.chtml中,我使用以下代码:
@RenderPage("~/Areas/MiniBlog/themes/MyTheme/Post.cshtml", post);
我宁愿做这样的事:
@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);