Wednesday 7 January 2015

different ways of extracting different parts of URL

Here is the sample how you can have different ways of extracting different parts of URL

EXAMPLE (Sample URL)

http://localhost:60527/WebSite1test/Default2.aspx?QueryString1=1&QuerrString2=2

CODE

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.AbsoluteUri);
Response.Write("<br/> " + HttpContext.Current.Request.Url.PathAndQuery);
OUTPUT

localhost
localhost:60527
/WebSite1test/Default2.aspx
/WebSite1test
http://localhost:60527/WebSite1test/Default2.aspx?QueryString1=1&QuerrString2=2
/WebSite1test/Default2.aspx?QueryString1=1&QuerrString2=2


If you have something like this:

"http://localhost:1234/Default.aspx?un=asdf&somethingelse=fdsa"
or like this:

"https://www.something.com/index.html?a=123&b=4567"
and you only want the part that a user would type in then this will work:

String strPathAndQuery = HttpContext.Current.Request.Url.PathAndQuery;
String strUrl = HttpContext.Current.Request.Url.AbsoluteUri.Replace(strPathAndQuery, "/");
which would result in these:

"http://localhost:1234/"
"https://www.something.com/"

No comments:

Post a Comment