ASP.NET MVC URL 解码

10

我有这样一个动作:

<%=Html.ActionLink("My_link", "About", "Home", new RouteValueDictionary { 
    { "id", "Österreich" } }, null)%>

这将生成以下链接:http://localhost:1855/Home/About/%C3%96sterreich

我想要一个链接看起来像这样 - localhost:1855/Home/About/Österreich

我已经尝试过了。

Server.HtmlDecode("Österreich")
HttpUtility.UrlDecode("Österreich") 

似乎两者都没有起到帮助的作用。我还能尝试什么来获得我想要的结果?


1
如果您没有使用umlaut对参数进行编码,是否会出现错误? - Dave Swersky
没有错误,网址正常工作。点击正确的操作方法并传递参数。我只想让网址变得“友好”。 - Bryan
你正在使用MVC 1.0吗?你是否和我一样使用ActionLink? - Bryan
一个副注:如果您只针对德语而不是更复杂的Unicode语言(如中文、日语、希伯来语、阿拉伯语),则可以考虑将字符“音译”(不确定这个词是否正确,用于简单删除字符上的变音符号和将“ä”转换为“ae”)。在德语搜索结果中,谷歌在进行高亮显示时会考虑到这种转换,另外,您的音译URL将在所有浏览器中正确显示。例如:http://www.google.co.uk/search?hl=en&q=schoen - Marek
4个回答

4

我认为这是你的浏览器(IE)的问题。

你的代码是正确的,不需要显式的UrlEncoding。

<%=Html.ActionLink("My_link", "About", "Home", new RouteValueDictionary { 
{ "id", "Österreich" } }, null)%>

ASP.NET MVC没有问题。在网络上可以看到Unicode URL,例如在支持Unicode的浏览器中打开http://he.wikipedia.org/wiki/%D7%9B%D7%A9%D7%A8%D7%95%D7%AA

例如,Chrome可以正常显示Unicode URL,但IE无法解码地址栏中的“特殊”Unicode字符。

这只是一个表面问题。

刚刚验证了一下 - FF可以很好地显示它们,但源代码显示编码的URL。 - womp
是的,看起来这似乎是IE的问题。感谢您的帮助。 - Bryan

2

根据RFC 1738 统一资源定位符(URL),仅支持 US-ASCII 字符集,所有其他字符必须进行编码。

2.2. URL Character Encoding Issues

URLs are sequences of characters, i.e., letters, digits, and special

characters. A URLs may be represented in a variety of ways: e.g., ink on paper, or a sequence of octets in a coded character set. The interpretation of a URL depends only on the identity of the characters used.

In most URL schemes, the sequences of characters in different parts of a

URL are used to represent sequences of octets used in Internet protocols. For example, in the ftp scheme, the host name, directory name and file names are such sequences of octets, represented by parts of the URL. Within those parts, an octet may be represented by the chararacter which has that octet as its code within the US-ASCII [20] coded character set.

In addition, octets may be encoded by a character triplet consisting of

the character "%" followed by the two hexadecimal digits (from "0123456789ABCDEF") which forming the hexadecimal value of the octet. (The characters "abcdef" may also be used in hexadecimal encodings.)

Octets must be encoded if they have no corresponding graphic

character within the US-ASCII coded character set, if the use of the corresponding character is unsafe, or if the corresponding character is reserved for some other interpretation within the particular URL scheme.

No corresponding graphic US-ASCII:

URLs are written only with the graphic printable characters of the

US-ASCII coded character set. The octets 80-FF hexadecimal are not used in US-ASCII, and the octets 00-1F and 7F hexadecimal represent control characters; these must be encoded.


1

我认为你想要一个非urlencode的URL是合理的,但我不认为工具实际上很容易做到这一点。


0
把生成的链接放在一个 <a> 标签内,使链接文本为非编码字符串,这样不就够好了吗?虽然在浏览器的 URL 地址栏中仍然看起来很糟糕,但你的用户界面会更漂亮一些。
此外,在 Firefox 中,当我将鼠标悬停在您的“丑陋”链接上时,状态栏显示的 URL 显示未编码版本,所以在那里它可能看起来也很不错。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接