Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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

like image 625
Zabavsky Avatar asked Mar 04 '26 17:03

Zabavsky


1 Answers

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;
}
like image 190
Daniel J.G. Avatar answered Mar 06 '26 10:03

Daniel J.G.