Skip to content

Instantly share code, notes, and snippets.

@kmlprtsng
Created August 14, 2013 14:54
Show Gist options
  • Save kmlprtsng/6231800 to your computer and use it in GitHub Desktop.
Save kmlprtsng/6231800 to your computer and use it in GitHub Desktop.
Get Base URL or a part of URL
//*************** Solution 1 **************/
// http://forums.asp.net/t/1466607.aspx/1
//http://forums.asp.net/t/1383898.aspx
//would return http://localhost:2013 or http://localhost:2013/ApplicationPath
return string.Format("{0}://{1}{2}",
HttpContext.Current.Request.Url.Scheme,
HttpContext.Current.Request.ServerVariables["HTTP_HOST"],
(HttpContext.Current.Request.ApplicationPath.Equals("/"))
? string.Empty : HttpContext.Current.Request.ApplicationPath
);
//*************** Solution 2 **************/
string baseUrl = Request.Url.Scheme + "://" + Request.Url.Authority +
Request.ApplicationPath.TrimEnd('/') + "/";
//*************** Solution 3 **************/
Request.Url.GetLeftPart(UriPartial.Authority)
Response.Write("<br/> " + HttpContext.Current.Request.Url.Host);
Response.Write("<br/> " + HttpContext.Current.Request.Url.Authority);
Response.Write("<br/> " + HttpContext.Current.Request.Url.AbsolutePath);
Response.Write("<br/> " + HttpContext.Current.Request.ApplicationPath);
Response.Write("<br/> " + HttpContext.Current.Request.Url.Host);
Response.Write("<br/> " + HttpContext.Current.Request.Url.AbsoluteUri);
Response.Write("<br/> " + HttpContext.Current.Request.Url.PathAndQuery);
/*Sample Url http://localhost:60527/WebSite1test/Default2.aspx?QueryString1=1&QuerrString2=2*/
/************* Output
localhost
localhost:60527
/WebSite1test/Default2.aspx
/WebSite1test
localhost
http://localhost:60527/WebSite1test/Default2.aspx?QueryString1=1&QuerrString2=2
/WebSite1test/Default2.aspx?QueryString1=1&QuerrString2=2
/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment