ASP.NET MVC4路由问题

Ale*_*rin 7 asp.net-mvc asp.net-mvc-routing asp.net-mvc-4

我已经搜索了Stack多年,阅读了MSDN文档并使用了Bing,但是看不出为什么这不起作用!我有以下相关代码+路线.调用的路径Browse工作正常,但路线的productCode参数Details总是等于零.如果我制作任何mods,我会继续获取'找不到资源'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
Run Code Online (Sandbox Code Playgroud)

路线是:

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}
    )
Run Code Online (Sandbox Code Playgroud)

Chr*_*ver 7

订单在定义路线时很重要.

当您要求site.com/details/abc123我认为它符合您的第一条路线.

你会得到

controller = "details"

action = "Index"

genre = "abc123"

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

切换两个route.MapRoute语句,它应该解决你的问题.

你的第二条路线确实设置了动作,info而不是index,但我认为这是一个错字?