ASP.NET MVC 6 application's virtual application root path

China☆狼群 提交于 2019-12-06 05:00:38

What you need can be achieved with @Url.Content("~/"), which will map "~" to your virtual application root path.

Having a look at the source code, it seems to do so using the HttpContext.Request.PathBase property:

public virtual string Content(string contentPath)
{
    if (string.IsNullOrEmpty(contentPath))
    {
        return null;
    }
    else if (contentPath[0] == '~')
    {
        var segment = new PathString(contentPath.Substring(1));
        var applicationPath = HttpContext.Request.PathBase;

        return applicationPath.Add(segment).Value;
    }

    return contentPath;
}

I use the following in MVC Core RC2:

Instead of "~/something" I use

Context.Request.PathBase + "/something"

or even more simply, just use "/something", which means starting a path with a slash tells asp core to start from the root.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!