Sunday 14 April 2013

How to get the URL of the current page in C#


How to get the URL of the current page in C#


following code returns values given in comment.

string url = HttpContext.Current.Request.Url.AbsoluteUri;
// http://localhost:1302/sitename/Default.aspx

string path = HttpContext.Current.Request.Url.AbsolutePath;
// /sitename/Default.aspx
string host = HttpContext.Current.Request.Url.Host;
// localhost 
if you just want the part between http:// and the first slash
string url = Request.Url.Host;
would return sitename.com if called from this page

No comments:

Post a Comment