actionLink razor helper中的html“id”,其中string是"格式错误"

时间:2015-01-16 14:43:08

标签: c# asp.net-mvc razor

如果我同时满足以下条件的条件,那么从MVC ActionLink发送一个字符串作为routeValues参数时遇到了问题:

  1. 参数名称为“id”
  2. 数据包含句号,例如mystring.bad
  3. 我看到this questionthis question引用了使用“id”的问题,但它们似乎不包括上述情况。

    下面我列出了不同的ActionLinks,其中列出了每个下面列出的网址:前两个工作,最后两个工作失败。

    @*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错误404.0 - 未找到”。

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

    注意:

    • 三个参数,即没有HtmlAttributes,也以同样的方式失败。
    • 我的路由表只是默认的MVC设置。

1 个答案:

答案 0 :(得分:1)

最好的我可以告诉,后两个URL失败,因为ASP.NET认为点之后的任何内容都是文件扩展名,所以它将文件处理程序移交给文件处理程序,因为文件处理程序找不到文件,因此404。

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

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

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

如果这些不起作用,您可以查看此answer