ASP.Net MVC 2.0:取消转义 URL 查询参数
我有以下 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
这将取决于 web.config 中的全球化元素:
或者如果您的网站是 UTF-8
但在这种情况下,URL 需要类似于
name=Cyan%c3%acde
。您应该始终使用 URL 帮助程序来生成 URL,以便对它们进行正确编码。This will depend on the globalization element in web.config:
Or if your site is 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.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 expectingUPDATE
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).
我在 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" \>
insystem.web
node, you must useHttpContext.Current.Server.UrlEncode
to urlencode \ decode cause in this way you are using globalization settings inweb.config
.Before using
UrlEncode
in currentHttpContext
I was usingHttpUtility.UrlDecode
that doesn't use current globalization settings.Worked for me!
我可以找到一些东西:
这些对您有用吗?
Some things I can find:
Any of these working for you?