How to read the query string params of a ASP.NET raw URL?
53,858
Solution 1
This is probably what you're after
Uri theRealURL = new Uri(HttpContext.Current.Request.Url.Scheme + "://" + HttpContext.Current.Request.Url.Authority + HttpContext.Current.Request.RawUrl);
string yourValue= HttpUtility.ParseQueryString(theRealURL.Query).Get("yourParm");
Solution 2
No need to go through the RawUrl
- the Request
object already contains a parsed version, using the Request.QueryString
property.
This is an indexed NameValueCollection
.
Solution 3
Try this:
string rawURL = HttpContext.Current.Request.ServerVariables["query_string"];
Author by
GilliVilla
Updated on July 09, 2022Comments
-
GilliVilla almost 2 years
I have a variable
string rawURL = HttpContext.Current.Request.RawUrl;
How do I read the query string parameters for this url?
-
james31rock almost 12 yearsreally? Is that all really necessary?
-
GilliVilla almost 12 years@james31rock yes..really :) Question wasn't that obvious that some geniuses have downvoted it ... rawurl needs to be handled this way. What others have mentioned is the default querystring.
-
james31rock almost 12 years@GilliVilla, you are correct if you are looking to retrieve the parameter from RawUrl. Why would you though? If you have HttpContext.Current.Request, all you need to do is HttpContext.Current.Request.QueryString["yourparam"]. Your making your code unreadable. That's why people gave you a down vote. I did not give you a down vote, but I understand why it happend.
-
NickG almost 10 years@james31rock In my case, because of URL rewriting. The visible URL in the browser and the RawUrl can be very different if you're using URL rewriting.
-
NickG almost 10 yearsHe's specifically asking how to do this on the RawUrl. The RawUrl querystring and the Request.QueryString are not related in some situations, such as if you're doing URL rewriting. The very fact he's using RawUrl is a strong hint he's using URL rewriting.
-
Daryl Teo over 9 yearsThis was really helpful. Should note that since you only want the QueryString, just use string.Format("http: //a.com{0}", Request.RawURL). The scheme and hostname really doesn't matter.
-
Manfred over 8 yearsIn the past I have also used
Request.Params
(suggested by @Piotr ) which is fine in some cases. In other cases I have switched toRequest.QueryString
as suggested by @Oded .Request.QueryString
doesn't trigger parameter validation, which you may want to avoid for example when you accept HTML as input.