Thursday, January 5, 2012 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;

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:

1 comment:

  1. Simple, concise list.
    Thanks for posting.


Comments will appear once they have been approved by the moderator