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

我怕爱的太早我们不能终老 提交于 2019-12-07 19:28:15

问题


How do I get the virtual root path of the application on the server?

In another words: how can I do the following in ASP.NET MVC 6?

HttpContext.Current.Request.ApplicationPath


回答1:


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;
}



回答2:


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.



来源:https://stackoverflow.com/questions/35082888/asp-net-mvc-6-applications-virtual-application-root-path

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