发布于 2015-05-02 02:53:32
如果您使用的是ASP.NET Core1或2,则可以使用Microsoft.AspNetCore.WebUtilities包中的Microsoft.AspNetCore.WebUtilities.QueryHelpers
执行此操作。
如果您使用的是NugetCore3.0或更高版本,WebUtilities
现在是nuget的一部分,不需要单独的ASP.NET包引用。
要将其解析为字典,请执行以下操作:
var uri = new Uri(context.RedirectUri);
var queryDictionary = Microsoft.AspNetCore.WebUtilities.QueryHelpers.ParseQuery(uri.Query);
注意,与System.Web中的ParseQueryString
不同,它在ASP.NET Core1.x中返回类型为IDictionary<string, string[]>
的字典,在ASP.NET Core2.x或更高版本中返回IDictionary<string, StringValues>
类型的字典,因此该值是一个字符串集合。这就是字典处理同名的多个查询字符串参数的方式。
如果要将参数添加到查询字符串中,可以在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);
使用.net Core2.2,您可以使用以下命令获取查询字符串
var request = HttpContext.Request;
var query = request.Query;
foreach (var item in query){
Debug.WriteLine(item)
}
您将获得一个键:值对的集合,如下所示
[0] {[companyName, ]}
[1] {[shop, ]}
[2] {[breath, ]}
[3] {[hand, ]}
[4] {[eye, ]}
[5] {[firstAid, ]}
[6] {[eyeCleaner, ]}
发布于 2017-04-14 15:17:21
获取绝对URI并仅使用ASP.NET核心包操作其查询字符串的最简单、最直观的方法只需几个简单的步骤即可完成:
安装程序包
PM> Install-Package Microsoft.AspNetCore.WebUtilities
PM> Install-Package Microsoft.AspNetCore.Http.Extensions
重要类
这里是我们将使用的两个重要类:QueryHelpers、StringValues和QueryBuilder。
《守则》
// Raw URI including query string with multiple parameters
var rawurl = "https://bencull.com/some/path?key1=val1&key2=val2&key2=valdouble&key3=";
// Parse URI, and grab everything except the query string.
var uri = new Uri(rawurl);
var baseUri = uri.GetComponents(UriComponents.Scheme | UriComponents.Host | UriComponents.Port | UriComponents.Path, UriFormat.UriEscaped);
// Grab just the query string part
var query = QueryHelpers.ParseQuery(uri.Query);
// Convert the StringValues into a list of KeyValue Pairs to make it easier to manipulate
var items = query.SelectMany(x => x.Value, (col, value) => new KeyValuePair<string, string>(col.Key, value)).ToList();
// At this point you can remove items if you want
items.RemoveAll(x => x.Key == "key3"); // Remove all values for key
items.RemoveAll(x => x.Key == "key2" && x.Value == "val2"); // Remove specific value for key
// Use the QueryBuilder to add in new items in a safe way (handles multiples and empty values)
var qb = new QueryBuilder(items);
qb.Add("nonce", "testingnonce");
qb.Add("payerId", "pyr_");
// Reconstruct the original URL with new query string
var fullUri = baseUri + qb.ToQueryString();
要及时了解任何更改,您可以在此处查看我的博客帖子:http://benjii.me/2017/04/parse-modify-query-strings-asp-net-core/
发布于 2015-05-02 02:56:45
HttpRequest
有一个Query
属性,它通过IReadableStringCollection
接口公开解析后的查询字符串:
/// <summary>
/// Gets the query value collection parsed from owin.RequestQueryString.
/// </summary>
/// <returns>The query value collection parsed from owin.RequestQueryString.</returns>
public abstract IReadableStringCollection Query { get; }
GitHub上的This discussion也指出了这一点。
https://stackoverflow.com/questions/29992848
复制相似问题