bbesase
bbesase

Reputation: 861

Getting URL of current page

I need to check if the current URL of the page contains a specific string or not. Now, I know you can do this by ClientQueryString.Contains. However, the page that I have to modify already inherit System.Web.UI.UserControl And I know in order to use ClientQueryString.Contains you must inherit System.Web.UI.Page

Is there anyway around this or another method that I can use since I can't inherit something else?

Upvotes: 0

Views: 2283

Answers (3)

EvilDr
EvilDr

Reputation: 9630

There is a full set of examples for Request.Url at this site To isolate the querystring you can do: Request.Url.GetComponents(UriComponents.Query, UriFormat.SafeUnescaped) Or Request.QueryString which is my assumption of what you're trying to do from your own code.

Upvotes: 1

SHEKHAR SHETE
SHEKHAR SHETE

Reputation: 6056

Please check the following to get different parts of URL

EXAMPLE (Sample URL)

http://localhost:60527/MyWeb/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
/MyWeb/Default2.aspx
/MyWeb
http://localhost:60527/MyWeb/Default2.aspx?QueryString1=1&QuerrString2=2
/MyWeb/Default2.aspx?QueryString1=1&QuerrString2=2 

You can copy paste above sample code & run it in asp.net web form application with different URL.

See here for more Details

Upvotes: 1

Tim
Tim

Reputation: 4101

Could you use HttpRequest.Url?

Request.URL documentation

Upvotes: 0

Related Questions