html actionLink razor 助手中的“id”,其中字符串属于 "bad format"

html “id” in actionLink razor helper where string is of a "bad format"

如果我同时满足以下两种情况,我在从 MVC ActionLink 将字符串作为 routeValues 参数发回时遇到问题:

  1. 参数名称是"id"
  2. 数据包含句号,例如mystring.bad

我看到 this question and this question 提到了使用 "id" 的问题,但它们似乎没有涵盖上述情况。

下面我列出了不同的 ActionLinks,它们生成的 url 都列在下面:前两个有效,后两个失败。

@*Links that work*@
@Html.ActionLink("Test OK", "About", new { id = "mystring" }, new { @class = "k-button" })
@*Above produces url http://localhost:55745/Home/About/mystring*@
@Html.ActionLink("Test OK", "About", new { stringId = "mystring.bad" }, new { @class = "k-button" })
@*Above produces url http://localhost:55745/Home/About/mystring?stringId=mystring.bad*@

@*Links that FAIL*@
@Html.ActionLink("FAIL: 4 param ActionLink", "About", new { id = "mystring.bad" }, new { @class = "k-button" })
@*Above produces url http://localhost:55745/Home/About/mystring.bad*@
@Html.ActionLink("FAIL: 5 param ActionLink", "About", "Home", new { id = "mystring.bad" }, new { @class = "k-button" })
@*Above produces url http://localhost:55745/Home/About/mystring.bad*@

最后两个失败产生以下错误:"HTTP Error 404.0 - Not Found"。

有人知道如何解决这个问题吗?我当然可以将参数名称更改为 "id" 以外的名称,但我想知道为什么这不起作用。

备注:

据我所知,后两个 URL 失败了,因为 ASP.NET 认为点后面的任何内容都是文件扩展名,所以它交给文件处理程序,它不能'找不到文件,因此出现 404。

(第二个 URL 成功,因为上述限制不适用于 QueryString 值)。

真正有趣的是,如果您在 URL 的末尾添加一个“/”,它就可以正常工作。

据说在您的 web.config 中设置 属性<httpRuntime relaxedUrlToFileSystemMapping="true"/> 适用于 .NET 4.0,但我无法在我的设置(.NET 4.5 和 MVC5)上使用它.

如果这些都不行,你可以看看这个answer