Translate

Thursday, January 5, 2012

asp.net get paths/urls

Get absolute path of a file :
string absoluteFilePath = Server.MapPath("~/Products.xml");


Get Complete url of the current page:
string completeUrl = HttpContext.Current.Request.Url.AbsoluteUri;
// "http://localhost:35876/Default.aspx"


Get the domain name (host header):
string domainName = HttpContext.Current.Request.Url.Host;
OR

string domainName =HttpContext.Current.Request.Headers["Host"]


// "localhost"



Get Relative path of the current page (relative to the root) :
string relativePath = HttpContext.Current.Request.Url.AbsolutePath;
// "/Default.aspx"


Further reading:
http://www.west-wind.com/weblog/posts/2009/Dec/21/Making-Sense-of-ASPNET-Paths

1 comment:

Comments will appear once they have been approved by the moderator