简体   繁体   中英

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:

@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);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM