23

I have a variable

string rawURL = HttpContext.Current.Request.RawUrl;

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

1

6 Answers 6

37

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"); 
Sign up to request clarification or add additional context in comments.

5 Comments

really? Is that all really necessary?
@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, 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 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.
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.
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.

2 Comments

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.
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.
1

Try this:

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

Comments

1

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

Comments

0

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

Comments

-6

This will solve your problem.....

string strReq = "";
strReq = HttpContext.Current.Request.RawUrl;
strReq = strReq.Substring(strReq.IndexOf('?') + 1);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.