If you are using ASP.NET Core 1 or 2, you can do this with Microsoft.AspNetCore.WebUtilities.QueryHelpers
in the Microsoft.AspNetCore.WebUtilities package.
If you are using ASP.NET Core 3.0 or greater, WebUtilities
is now part of the ASP.NET SDK and does not require a separate nuget package reference.
To parse it into a dictionary:
var uri = new Uri(context.RedirectUri);
var queryDictionary = Microsoft.AspNetCore.WebUtilities.QueryHelpers.ParseQuery(uri.Query);
Note that unlike ParseQueryString
in System.Web, this returns a dictionary of type IDictionary<string, string[]>
in ASP.NET Core 1.x, or IDictionary<string, StringValues>
in ASP.NET Core 2.x or greater, so the value is a collection of strings. This is how the dictionary handles multiple query string parameters with the same name.
If you want to add a parameter on to the query string, you can use another method on QueryHelpers
:
var parametersToAdd = new System.Collections.Generic.Dictionary<string, string> { { "resource", "foo" } };
var someUrl = "http://www.google.com";
var newUri = Microsoft.AspNetCore.WebUtilities.QueryHelpers.AddQueryString(someUrl, parametersToAdd);
Using .net core 2.2 you can get the query string using
var request = HttpContext.Request;
var query = request.Query;
foreach (var item in query){
Debug.WriteLine(item)
}
You will get a collection of key:value pairs - like this
[0] {[companyName, ]}
[1] {[shop, ]}
[2] {[breath, ]}
[3] {[hand, ]}
[4] {[eye, ]}
[5] {[firstAid, ]}
[6] {[eyeCleaner, ]}
Microsoft.AspNet.WebUtilties
may be theMono.HttpUtility
library. – FlamingHttpUtility
andParseQueryString
method. – Palingenesis