Pergunta

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?

Foi útil?

Solução

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.

Outras dicas

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

Could you use HttpRequest.Url?

Request.URL documentation

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top