ASP.NET MVC解析URL查询部分

发布于 2024-10-02 04:13:21 字数 897 浏览 0 评论 0原文

我想要将 URL 的 ?a=5&b=8 部分转换为字典 { a:5, b:8 }。

.net中有这样的方法还是我自己的?

我需要解析查询部分,以便我可以实现我自己的 URL 解码。 问题是 Firefox 根据使用的字母表使用不同的编码(UTF8 或 ISO-8859-1)。

示例:

Firefox 编码如下:

-                                          v   v
http://localhost:8041/Reforge.aspx?name=ArcânisГ 
Firefox turns into  
http://localhost:8041/Reforge.aspx?name=Arc%C3%A2nis%D0%93`  

请注意,UTF8 编码用于两种非 ASCII 字符。

-                                          v
http://localhost:8041/Reforge.aspx?name=Arcâ
Firefox turns into
http://localhost:8041/Reforge.aspx?name=Arc%E2

请注意,ISO-8859-1 (Latin-1) 编码用于非 ASCII 字符。

这是一个相关问题: ASP.NET MVC 不理解混合 url 编码 (UTF-8/Latin-1)

I want something to turn ?a=5&b=8 part of a URL into a dictionary { a:5, b:8 }.

Is there such method in .net or am I on my own?

I need to parse query part so that I can implement my own URL decoding.
The problem is that Firefox uses different encoding (UTF8 or ISO-8859-1) depending on the used alphabet.

Example:

Firefox encodes as following:

-                                          v   v
http://localhost:8041/Reforge.aspx?name=ArcânisГ 
Firefox turns into  
http://localhost:8041/Reforge.aspx?name=Arc%C3%A2nis%D0%93`  

Notice that UTF8 encoding is used for both non-ASCII characters.

-                                          v
http://localhost:8041/Reforge.aspx?name=Arcâ
Firefox turns into
http://localhost:8041/Reforge.aspx?name=Arc%E2

Notice that ISO-8859-1 (Latin-1) encoding is used for the non-ASCII character.

Here is a related question: ASP.NET MVC does not understand mixed url encoding (UTF-8/Latin-1)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

旧人哭 2024-10-09 04:13:21

Request.QueryString 是一个类似于字典的 NameValueCollection。

它可以包含同一键的多个值。

如果“?a=5&b=8”不是来自查询字符串,您可以使用HttpUtility.ParseQueryString方法来获取NameValueCollection

如果您想要一个真正的字典:

var d = new Dictionary<String,Object>();
this.Request.QueryString.CopyTo(d);

Request.QueryString is a NameValueCollection that is like a dictionary.

It can contain multiple values for the same key.

If "?a=5&b=8" not come from querystring you can use HttpUtility.ParseQueryString method to get a NameValueCollection

If you want a true dictionary:

var d = new Dictionary<String,Object>();
this.Request.QueryString.CopyTo(d);
带刺的爱情 2024-10-09 04:13:21

也许是一个自定义模型绑定器来处理字典?看起来依赖查询字符串虽然完全有效,但却违背了框架的精神。它还可能使测试您的操作变得更加困难。

我的第一个想法是,默认的模型绑定器工具已经处理了这个问题,但事实似乎并非如此。也就是说,我还没有尝试过,但这是我开始的地方:

ASPnet MVC 的直观字典模型绑定器

Maybe a custom modelbinder to handle dictionaries? It seems like relying on the querystring, while perfectly valid, goes against the spirit of the framework. It also may make testing your action a little more difficult.

My first thought was that this was already handled by the default modelbinder facilites, but this doesn't appear to be the case. That said, I haven't tried it but this is where I'd start:

An Intuitive Dictionary Model Binder for ASPnet MVC

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文