Monday, 20 April 2015

ASP.NET - Current Page Url with QueryString

You might need to extract different values from url.
It might be the path or the current page or values in the querystring.
Here is a simple reference to different uri based info available to you out of the box.

SAMPLE URL:
http://habeeb.in/UrlTest/Default.aspx?QueryString1=1&QueryString2=2

CODE:
Response.Write( Request.Url.Host );
Response.Write( Request.Url.AbsolutePath );
Response.Write( Request.ApplicationPath );
Response.Write( Request.Url.AbsoluteUri );
Response.Write( Request.Url.PathAndQuery );

OUTPUT:
habeeb.in
/UrlTest/Default.aspx
/UrlTest
http://habeeb.in/UrlTest/Default.aspx?QueryString1=1&QueryString2=2
/UrlTest/Default.aspx?QueryString1=1&QueryString2=2

The above sample "Request.Url" is when you use in ASP.NET Code behind.
If you use it within a class library then you will have to refer it as ""HttpContext.Current.Request.Url".

No comments:

Post a Comment