ASP.Net MVC 2.0:取消转义 URL 查询参数

发布于 2024-10-02 12:40:27 字数 359 浏览 4 评论 0原文

我有以下 URL:

http://localhost:8041/Reforge.aspx?name=Cyan%ECde&realmId=1

注意 值中的 %EC name 参数。

%EC = 236 = ì (igrave)

在我的操作方法中:

public ActionResult Index(string name, int realmId) {...}

name[4] 是一个代码为 65533 (0xFFFD) 的字符。我做错了什么?

I have following URL:

http://localhost:8041/Reforge.aspx?name=Cyan%ECde&realmId=1

notice %EC in the value of name parameter.

%EC = 236 = ì (igrave)

In my action method:

public ActionResult Index(string name, int realmId) {...}

name[4] is a character with code 65533 (0xFFFD). What am I doing wrong?

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

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

发布评论

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

评论(4

不必在意 2024-10-09 12:40:27

这将取决于 web.config 中的全球化元素:

<globalization requestEncoding="iso-8859-1" />

或者如果您的网站是 UTF-8

<globalization requestEncoding="utf-8" />

但在这种情况下,URL 需要类似于 name=Cyan%c3%acde。您应该始终使用 URL 帮助程序来生成 URL,以便对它们进行正确编码。

This will depend on the globalization element in web.config:

<globalization requestEncoding="iso-8859-1" />

Or if your site is UTF-8

<globalization requestEncoding="utf-8" />

But in this case the url needs to look like name=Cyan%c3%acde. You should always use URL helpers to generate urls so that they are properly encoded.

但可醉心 2024-10-09 12:40:27

HttpUtility.UrlDecode("%EC") 将该字符 (65533) 作为其输出。

HttpUtility.UrlEncode("ì") 生成 "%c3%ac"

您是如何生成此 %EC 的?看起来您的编码不起作用,因为 ASP.NET 正在等待

UPDATE

您说您只是输入 http://localhost:8041/Reforge.aspx?name=Cyanìde& realmId=1 进入您的浏览器,但编码不正确。我建议您一开始就不应该将其输入到浏览器中。如果您要生成该 URL,则需要对其进行编码(以便它呈现为 )。当鼠标悬停在 Firefox 上时,Firefox 会显示 ì,但在单击或复制时会给出编码版本。

如果用户在 URL 中输入任意 unicode,您就没有一个好的方法来处理该问题(因为他们实际上向您发送了无效请求)。

HttpUtility.UrlDecode("%EC") gives that character (65533) as its output.

HttpUtility.UrlEncode("ì") produces "%c3%ac"

How are you generating this %EC? It looks like your encoding isn't working as ASP.NET is expecting

UPDATE

You say that you're just entering http://localhost:8041/Reforge.aspx?name=Cyanìde&realmId=1 into your browser and that it's not encoding correctly. I would suggest that you shouldn't be entering that into your browser in the first place. If you're generating that URL, you need to encode it (so that it renders as <a href="http://localhost:8041/Reforge.aspx?name=Cyan%c3%acde&realmId=1">). Firefox will show this with the ì when hovering over, but will give the encoded version when clicked or copied.

If users are typing arbitrary unicode into a URL, there's not a good way for you to handle that (since they're effectually sending you invalid requests).

还如梦归 2024-10-09 12:40:27

我在 ASP.NET 和 js 中遇到了类似的 url 编码问题(ì 必须编码为 %EC 才能工作)。

如果您在 system.web 节点中设置 ,则必须使用HttpContext.Current.Server.UrlEncode 到 urlencode\decode 会导致您在 web.config 中使用全球化设置。

在当前 HttpContext 中使用 UrlEncode 之前,我使用的是不使用当前全球化设置的 HttpUtility.UrlDecode

为我工作!

I faced a similar problem with url encoding (ì must be encoded as %EC to work) in ASP.NET and js.

If you set <globalization requestEncoding="ISO-8859-1" responseEncoding="ISO-8859-1" \> in system.web node, you must use HttpContext.Current.Server.UrlEncode to urlencode \ decode cause in this way you are using globalization settings in web.config.

Before using UrlEncode in current HttpContext I was using HttpUtility.UrlDecode that doesn't use current globalization settings.

Worked for me!

小猫一只 2024-10-09 12:40:27

我可以找到一些东西:

HttpServerUtility.UrlDecode(您的参数)

HttpUtility.UrlDecode(您的参数)

Server.UrlDecode(你的参数)

这些对您有用吗?

Some things I can find:

HttpServerUtility.UrlDecode(your param)

HttpUtility.UrlDecode(your param)

Server.UrlDecode(your param)

Any of these working for you?

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