Web developers wiki ASP.NET Sitecore Sharepoint Kentico by Evident Interactive



The tilde in ASP.NET is a shortcut for:

C#:
System.Web.HttpRuntime.AppDomainAppVirtualPath

Example:

C#:
protected override void Render(HtmlTextWriter writer)
{
    MemoryStream ms = new MemoryStream();
    StreamWriter sw = new StreamWriter(ms);
    HtmlTextWriter htw = new HtmlTextWriter(sw);

base.Render(htw); htw.Flush(); ms.Position = 0;

TextReader tr = new StreamReader(ms); string output = tr.ReadToEnd(); string newOutput = ReplaceWithAppPath(output); writer.Write(newOutput); htw.Close(); sw.Close(); ms.Close(); }

public static string ReplaceWithAppPath(string str) { string appPath = HttpContext.Current.Request.ApplicationPath;

//Ensure the app path ends w/ a slash if(!appPath.EndsWith("/")) appPath += "/";

return str.Replace("~/", appPath); }

Edit

The official way to handle the tilde however is: Edit

UrlPath.MakeVirtualPathAppAbsolute()

Example:

C#:
UrlPath.MakeVirtualPathAppAbsolute("~/example.aspx");

 © Evident Interactive BV