I have a variable
string rawURL = HttpContext.Current.Request.RawUrl;
How do I read the query string parameters for this url?
I have a variable
string rawURL = HttpContext.Current.Request.RawUrl;
How do I read the query string parameters for this url?
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");
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.
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.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");