ASP.NET MVC4路由问题

7

我在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}
    )
1个回答

7
定义路由时顺序很重要。当您请求 site.com/details/abc123 时,我认为它匹配您的第一个路由。
您将得到 controller = "details" action = "Index" genre = "abc123" 这就是为什么您的productCode为空的原因。
交换两个route.MapRoute语句,应该可以解决您的问题。
您的第二个路由确实将操作设置为info而不是index,但我假设这是一个打字错误?

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接