-
-
Save JewettCitySoftwareCorporation/783d788da20405240f83a224843722ab to your computer and use it in GitHub Desktop.
Get Base URL or a part of URL
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//*************** 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) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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