【问题标题】:Asp.Net MVC - HTML.ActionLink - HowToGoTo - Client/Index/A - routeAsp.Net MVC - HTML.ActionLink - HowToGoTo - 客户端/索引/A - 路由
【发布时间】:2011-09-08 14:40:37
【问题描述】:

这是我的路线(工作)

    routes.MapRoute("ClientIndexAndSubstringChar", _
                    "{controller}/{action}/{FirstChar}", _
                    New With {.char = "[a-zA-Z0-9]"})

这是在我的控制器中(工作)

   Function Index(Optional ByVal KlantenSet As List(Of Domain.Slave.Klant) = Nothing, Optional FirstChar As Char = Nothing, Optional format As String = "html") As ActionResult
     //Some Code
    End Function

这是我的视图(链接不正确)

@If ViewBag.ClientsLetters IsNot Nothing Then
  For Each ClientLetter As String In ViewBag.ClientsLetters
    @<div class="ClientsLetter">
      @Html.ActionLink(ClientLetter, "Index", "Client", New With {.FirstChar = ClientLetter})
    </div>
   Next
 End If

ActionLink 错误....

我应该怎么做才能解决它? 我的链接链接到: http://localhost:52254/Client/Index.html?Length=6 代替 http://localhost:52254/Client/Index/A

【问题讨论】:

    标签: asp.net-mvc asp.net-mvc-3 asp.net-mvc-routing


    【解决方案1】:

    您的操作链接

    @Html.ActionLink(ClientLetter, "Index", "Client", New With {.FirstChar = ClientLetter})
    

    这里的参数匹配这个http://msdn.microsoft.com/en-us/library/dd504972.aspx

    应该是这样的

    @Html.ActionLink(ClientLetter, "Index", "Client", New With {.FirstChar = ClientLetter}, Null)
    

    这些参数匹配这个http://msdn.microsoft.com/en-us/library/dd493068.aspx

    对于 MapRoute,我认为您可以这样做:

    routes.MapRoute("ClientIndexAndSubstringChar", _
                    "{controller}/{action}/{FirstChar}")
    

    只需去掉最后一个参数。试一试,看看它能做什么。

    【讨论】:

    • 你说得对,但是显示的是localhost:52254/Client/Index.html?FirstChar=A,是不是很难显示为localhost:52254/Client/Index/A?因为通往那里的路线有效...... :)
    • 我对VB有点陌生,所以这有点难。如果你想让它看起来那样,你需要在 MapRoute 中做一些事情让它以那样的方式显示。 ActionLink 的链接基于我相信的 MapRoute。
    • 感谢您的工作。我更改了路由器并将 /Client/Index/A 更改为 /Client/A 以使其更容易,现在一切正常。谢谢!
    猜你喜欢
    • 1970-01-01
    • 2011-03-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-07-22
    • 1970-01-01
    • 2011-02-15
    相关资源
    最近更新 更多