22

I have a variable

string rawURL = HttpContext.Current.Request.RawUrl;

How do I read the query string parameters for this url?

GilliVilla
  • 4,998
  • 11
  • 55
  • 96

6 Answers6

36

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"); 
Shankar R10N
  • 4,926
  • 1
  • 21
  • 24
  • 4
    really? Is that all really necessary? – james31rock Jul 26 '12 at 20:13
  • 2
    @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. – GilliVilla Jul 26 '12 at 23:03
  • @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. – james31rock Jul 27 '12 at 12:47
  • 3
    @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 Jul 09 '14 at 08:57
  • 1
    This 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. – Daryl Teo Nov 19 '14 at 04:51
14

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.

Oded
  • 489,969
  • 99
  • 883
  • 1,009
  • 3
    He'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. – NickG Jul 09 '14 at 08:59
  • In the past I have also used `Request.Params` (suggested by @Piotr ) which is fine in some cases. In other cases I have switched to `Request.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. – Manfred Oct 05 '15 at 20:18
1

Try this:

string rawURL = HttpContext.Current.Request.ServerVariables["query_string"];

0

There is Params property on Request object that will let you do it easily. You don't have to parse it yourself.

Piotr Perak
  • 10,718
  • 9
  • 49
  • 86
0

In .NET Core there are multiple ways to access HttpContext like IHttpContextAccessor.

https://learn.microsoft.com/en-us/aspnet/core/fundamentals/http-context?view=aspnetcore-5.0

When you have the Context you can simply use this method:

httpContext.Request.QueryString.Value

Usage:

URL: https://localhost:44335/test?key=123

var key = System.Web.HttpUtility.ParseQueryString(httpContext.Request.QueryString.Value).Get("key");

enter image description here

Ogglas
  • 62,132
  • 37
  • 328
  • 418
-5

This will solve your problem.....

string strReq = "";
strReq = HttpContext.Current.Request.RawUrl;
strReq = strReq.Substring(strReq.IndexOf('?') + 1);
chiccodoro
  • 14,407
  • 19
  • 87
  • 130