MVC 参数中的加号 (+) 在 IIS 7.0 上导致 404 [英] Plus (+) in MVC Argument causes 404 on IIS 7.0
问题描述
我有一个 MVC 路由,它在运行 IIS 的临时服务器上给我带来了麻烦.我在本地运行 Visual Studio 2010 的开发服务器.
I have an MVC route that is giving me hell on a staging server running IIS. I am running Visual Studio 2010's development server locally.
这是一个实际适用于我的开发框的示例 URL:
Here is a sample URL that actually works on my dev box:
Root/CPUBoards/Full+Size
Results
Server Error404 - File or directory not found.
The resource you are looking for might have been removed, had its name changed, or is temporarily unavailable.
这是我看到的完整行为.
Here is the complete behaviour I am seeing.
本地主机:
Root/CPUBoards/Full Size - Resolves
Root/CPUBoards/Full%20Size - Resolves
Root/CPUBoards/Full+Size - Resolves
带有 IIS 7.0 的暂存服务器:
Staging Server with IIS 7.0:
Root/CPUBoards/Full Size - Resolves
Root/CPUBoards/Full%20Size - Resolves
Root/CPUBoards/Full+Size - 404 Not Found Error.
有什么想法吗?出于多种原因,我需要使用编码版本...不会浪费您的时间.
Any ideas? I need to work with the encoded version for several reasons... won't waste your time with them.
HttpUtility.UrlEncode("Full Size") 返回带有加号的版本... Full+Size.这适用于我的开发箱,但不适用于登台服务器.我宁愿让它在服务器上运行,因为我已经测试了其他所有内容并在本地运行,但我不知道从哪里开始查看服务器配置以使其具有相同的行为.
HttpUtility.UrlEncode("Full Size") returns the version with the plus sing... Full+Size. This works on my dev box, but not on the staging server. I would prefer to just get it working on the server, since I already have everything else tested and working locally, but I have no idea where to start looking on the server configuration to get it to behave the same way.
谢谢!
推荐答案
+
仅在 application/x-www-form-urlencoded
中有空格的特殊含义> 数据,例如 URL 的查询字符串部分.
+
only has the special meaning of being a space in application/x-www-form-urlencoded
data such as the query string part of a URL.
在 URL 的其他部分,如路径组件,+
字面意思是加号.因此,将 Full+Size
解析为未编码的名称 Full Size
不应该在任何地方工作.
In other parts of the URL like path components, +
literally means a plus sign. So resolving Full+Size
to the unencoded name Full Size
should not work anywhere.
路径组件中唯一正确的空格形式是 %20
.(当您输入实际空格时它仍然有效,因为浏览器会发现错误并为您更正.)%20
也适用于表单 URL 编码的数据,因此通常最安全的是用那个.
The only correct form of a space in a path component is %20
. (It still works when you type an actual space because the browser spots the error and corrects it for you.) %20
also works in form-URL-encoded data as well, so it's generally safest to always use that.
遗憾的是,HttpUtility.UrlEncode
的名称具有误导性.它在其输出中生成 +
而不是 %20
,因此它实际上是一个表单 URL 编码器而不是标准 URL 编码器.不幸的是,我不知道 ASP.NET 函数可以真正对 URL 编码"字符串以用于路径,所以我只能推荐将 +
的字符串替换为 %20
编码后.
Sadly HttpUtility.UrlEncode
is misleadingly-named. It produces +
in its output instead of %20
, so it's really a form-URL-encoder and not a standard URL-encoder. Unfortunately I don't know of an ASP.NET function to "really URL-encode" strings for use in a path, so all I can recommend is doing a string replace of +
to %20
after encoding.
或者,避免在路径部分使用空格,例如.用 -
替换它们.通常将slug"标题插入到 URL 中,将它们简化为简单的字母数字和安全"标点符号,以避免用丑陋的 %nn
序列填充 URL.
Alternatively, avoid using spaces in path parts, eg. by replacing them with -
. It's common to ‘slug’ titles being inserted to URLs, reducing them to simple alphanumerics and ‘safe’ punctuation, to avoid filling the URL with ugly %nn
sequences.
这篇关于MVC 参数中的加号 (+) 在 IIS 7.0 上导致 404的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!