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
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;
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With