当字符 %20 出现在参数 url 之间时,我的 MVC 路由将停止考虑该字符串。
这是为什么,我该如何处理我的 URL 中的“%20”字符?
示例网址
http://localhost:40494/ListContents/Delete/asdf%20/5430f394...
public ActionResult Delete(string DNSName, Guid id)
{...}
routes.MapRoute(
"Delete", // Route name
"ListContents/Delete/{DNSName}/{id}", // URL with parameters
new { controller = "ListContents", action = "Delete" } // Parameter defaults
);
但是 以下两个 URL 都可以正常工作
http://localhost:40494/ListContents/Delete/asdf%20SOMETHING_HERE/5430f394...
http://localhost:40494/ListContents/Delete/%20asdf/5430f394-946c-4f82-ac13-9d5efafe9127
最佳答案
如果 URL 任何部分末尾的下一个斜线之前有空格,它会抛出 HttpException
。在System.Web.Util.FileUtil.CheckSuspiciousPhysicalPath()
由 MVC 处理的方法,您将获得 HTTP 404 响应。
您可以通过选中 Throw
的复选框来验证自己在:
-
Visual Studio
-
Debug
-
Exceptions
-
Common Language Runtime Exceptions
通常您的 URL 中不应有空格。我亲自格式化我的网址,所有空格都变成破折号 (-)。
关于asp.net-mvc - %20 后跟斜杠后跟更多数据导致 ASP.NET MVC 3 路由失败?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4853626/