ASP.NET Get Current URL

By Mr Flo at May 28, 2010 21:21
Filed Under: ASP.NET, Silverlight

I often forget the correct syntax to find the current URL, so I posted it to remember it... Always usefull

Request.Url.AbsoluteUri

 public static string BaseSiteUrl
{
get
{
HttpContext context = HttpContext.Current;
string baseUrl = context.Request.Url.Scheme + "://" + context.Request.Url.Authority + context.Request.ApplicationPath.TrimEnd('/') + '/';
return baseUrl;
}
}

OR
public string BaseSiteUrl()
()
{
   
if(HttpContext.Current.Request.ApplicationPath == "/")
       
return "http://" + HttpContext.Current.Request.Url.Host;
   
else
       
return "http://" + HttpContext.Current.Request.Url.Host + HttpContext.Current.Request.ApplicationPath;
}

intersting link:

More to come

Comments are closed

About the author

Analyst programmer passionnate about Internet technologies and business around it

For consultant service related to .NET,ASP.NET, MVC, Umbraco, etc contact me

Read more on my linkedIn

Page List