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
.
Try this:
string rawURL = HttpContext.Current.Request.ServerVariables["query_string"];
There is Params property on Request object that will let you do it easily. You don't have to parse it yourself.
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");
This will solve your problem.....
string strReq = "";
strReq = HttpContext.Current.Request.RawUrl;
strReq = strReq.Substring(strReq.IndexOf('?') + 1);