【问题标题】:ASP.NET MVC4 routing issueASP.NET MVC4 路由问题
【发布时间】:2012-06-21 23:38:23
【问题描述】:

我已经在 Stack 上搜索了很长时间,阅读了 MSDN 文档并使用了 Bing,但不明白为什么这不起作用!我在下面有相关代码+路线。名为Browse 的路由运行良好,但Details 路由的productCode 参数始终等于无。如果我制作任何模组,我会不断收到“找不到资源”404 页面。

' Lives in controller called 'Details'
' Usage: site.com/details/abc123
Function Index(productCode As String) As ActionResult

' Lives in controller called 'Browse'    
' Usage: site.com/browse/scifi/2
Function Index(genre As String, Optional page As Integer = 1) As ActionResult

路线是:

routes.MapRoute( _
        "Browse", _
        "{controller}/{genre}/{page}", _
        New With {.controller = "Browse", .action = "Index", .id = UrlParameter.Optional, .page = UrlParameter.Optional}
    )

    routes.MapRoute( _
        "Details", _
        "details/{productCode}", _
        New With {.controller = "Details", .action = "Info", .productCode = UrlParameter.Optional}
    )

【问题讨论】:

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


    【解决方案1】:

    在定义路线时,顺序确实很重要。

    当您请求site.com/details/abc123 时,我认为它与您的第一条路线相匹配。

    你会得到

    controller = "details"

    action = "Index"

    genre = "abc123"

    这就是您的 productCode 为空的原因。

    切换两个route.MapRoute 语句,它应该可以解决您的问题。

    您的第二条路线确实将操作设置为info 而不是index,但我假设这是一个错字?

    【讨论】:

      猜你喜欢
      • 2015-12-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-07-10
      相关资源
      最近更新 更多