14

Where does ASP.NET virtual path resolve the tilde ~ in the links, for example

<link rel="stylesheet" type="text/css" href="~/Css/Site.css" />

Does it redirect, or RedirectToAction in ASP.NET MVC?

FOX 9000
  • 123
  • 1
  • 1
  • 6
andres descalzo
  • 14,887
  • 13
  • 64
  • 115

3 Answers3

22

It gets it from here:

VirtualPathUtility.ToAbsolute(contentPath, httpContext.Request.ApplicationPath);

Here is the reflector output for PathHelpers class in System.Web.Mvc DLL:

private static string GenerateClientUrlInternal(HttpContextBase httpContext, string contentPath)
{
    if (string.IsNullOrEmpty(contentPath))
    {
        return contentPath;
    }
    if (contentPath[0] == '~')
    {
        string virtualPath = VirtualPathUtility.ToAbsolute(contentPath, httpContext.Request.ApplicationPath);
        string str2 = httpContext.Response.ApplyAppPathModifier(virtualPath);
        return GenerateClientUrlInternal(httpContext, str2);
    }
    NameValueCollection serverVariables = httpContext.Request.ServerVariables;
    if ((serverVariables == null) || (serverVariables["HTTP_X_ORIGINAL_URL"] == null))
    {
        return contentPath;
    }
    string relativePath = MakeRelative(httpContext.Request.Path, contentPath);
    return MakeAbsolute(httpContext.Request.RawUrl, relativePath);
}
Aliostad
  • 80,612
  • 21
  • 160
  • 208
  • my question is based on this problem "http://stackoverflow.com/questions/4563043/root-path-with-tilde-does-not-change-upper-case-to-lower-case" to see the link "http://msdn.microsoft.com/en-us/library/system.web.virtualpathutility.aspx ", I think the problem is not in asp.net-mvc, but in IIS. Thanks – andres descalzo Dec 30 '10 at 14:57
2

See MSDN:Web Project Paths

ASP.NET includes the Web application root operator (~), which you can use when specifying a path in server controls. ASP.NET resolves the ~ operator to the root of the current application. You can use the ~ operator in conjunction with folders to specify a path that is based on the current root.

Basically, the purpose of the tilde is so that you can have a path that resolves properly even if you deploy your website to different places. Relative paths cannot accomplish this easily because controls may be rendered in different folders within your website. Absolute paths cannot accomplish this because your website may be deployed to different locations -- if nothing else, this is the case for test deployments made locally vs release deployments to the live server.

Server.MapPath can be used for similar reasons.

Brian
  • 25,523
  • 18
  • 82
  • 173
1

ASP.Net translates the tilde(~) with the application's root directory in every runat=server control. It is the equivalent for the HttpRuntime.AppDomainAppVirtualPath Property.

Tim Schmelter
  • 450,073
  • 74
  • 686
  • 939
  • No, it is not equivalent. AppDomainAppVirtualPath is "The virtual path of the directory that contains the application hosted in the current application domain" (virtual directory). It is certainly not the same with application's root directory, at least from IIS7. – Hoàng Long Feb 17 '16 at 07:11