首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >在.NET核心中解析和修改查询字符串

在.NET核心中解析和修改查询字符串
EN

Stack Overflow用户
提问于 2015-05-02 02:31:55
回答 6查看 92.2K关注 0票数 136

我得到了一个包含查询字符串的绝对URI。我希望安全地将一个值附加到查询字符串,并更改现有的参数。

我不喜欢使用&foo=bar,或者使用正则表达式,URI转义是很棘手的。相反,我想使用一种内置的机制,我知道这样做是正确的,并且可以处理转义。

I‘s found a ton of answers that all use HttpUtility。然而,这是ASP.NET核心,不再有System.Web程序集,因此不再有HttpUtility

在以核心运行时为目标的同时,在ASP.NET核心中执行此操作的适当方式是什么?

EN

回答 6

Stack Overflow用户

回答已采纳

发布于 2015-05-02 02:53:32

如果您使用的是ASP.NET Core1或2,则可以使用Microsoft.AspNetCore.WebUtilities包中的Microsoft.AspNetCore.WebUtilities.QueryHelpers执行此操作。

如果您使用的是NugetCore3.0或更高版本,WebUtilities现在是nuget的一部分,不需要单独的ASP.NET包引用。

要将其解析为字典,请执行以下操作:

代码语言:javascript
复制
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上使用其他方法

代码语言:javascript
复制
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,您可以使用以下命令获取查询字符串

代码语言:javascript
复制
var request = HttpContext.Request;
var query = request.Query;
foreach (var item in query){
   Debug.WriteLine(item) 
}

您将获得一个键:值对的集合,如下所示

代码语言:javascript
复制
[0] {[companyName, ]}
[1] {[shop, ]}
[2] {[breath, ]}
[3] {[hand, ]}
[4] {[eye, ]}
[5] {[firstAid, ]}
[6] {[eyeCleaner, ]}
票数 179
EN

Stack Overflow用户

发布于 2017-04-14 15:17:21

获取绝对URI并仅使用ASP.NET核心包操作其查询字符串的最简单、最直观的方法只需几个简单的步骤即可完成:

安装程序包

PM> Install-Package Microsoft.AspNetCore.WebUtilities

PM> Install-Package Microsoft.AspNetCore.Http.Extensions

重要类

这里是我们将使用的两个重要类:QueryHelpersStringValuesQueryBuilder

《守则》

代码语言:javascript
复制
// 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/

票数 45
EN

Stack Overflow用户

发布于 2015-05-02 02:56:45

HttpRequest有一个Query属性,它通过IReadableStringCollection接口公开解析后的查询字符串:

代码语言:javascript
复制
/// <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也指出了这一点。

票数 22
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/29992848

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档