ASP.NET MVC 2 - Problem with Request encoding

删除回忆录丶 提交于 2019-12-11 03:14:17

问题


Hello!

There is a controller and an action which receives one param through GET, approximately here so:

www.site.com/controller/action/?query=параметр <- Russian word

Problem:

Example 1: www.site.com/controller/action/?query=Пример <- Russian word

Example 2: www.site.com/controller/action/?query=Example

Reading param:

var param = Request.QueryString["query"];

Result 1:

  param = "������"

The data from debugger:

Request.RawUrl = "/controller/action/?q=%CF%F0%E8%EC%E5%F0"
QueryString = {q=%ufffd%ufffd%ufffd%ufffd%ufffd%ufffd}

Result 2:

param = "Example"

The data from debugger:

Request. RawUrl = "/controller/action/?q=Example"
QueryString = {q=Example}

ContentEncoding is setted into UTF-8.

Web.config:
<globalization requestEncoding="utf-8" responseEncoding="utf-8"
           fileEncoding="utf-8" />

Question: How can i correctly get param with russian word?


回答1:


You should never use Russian words in URI (и даже не стоит пробовать). You should encode them.

RFC 1738: Uniform Resource Locators (URL) specification

..Only alphanumerics [0-9a-zA-Z], the special characters "$-_.+!*'()," [not including the quotes - ed], and reserved characters used for their reserved purposes may be used unencoded within a URL.

If your users going to enter urls themselves in russian - (for searching) you can try UrlDecode Request.Url




回答2:


You need to UrlEncode the querystring values.




回答3:


HttpUtility.UrlDecode(Request.QueryString["q"], Encoding.Default) 

solves the problem.



来源:https://stackoverflow.com/questions/3488977/asp-net-mvc-2-problem-with-request-encoding

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!