2012-05-25 14 views
7

Stackを検索してMSDNのドキュメントを読んで、Bingを使用しましたが、なぜ動作しないのか分かりません。私は以下の関連するコードを得ました。 Browseと呼ばれるルートはうまく動作しますが、DetailsルートのproductCodeパラメータは常に無関係です。私が何か改造をしたら、私は404ページの "リソースが見つかりません"という情報を得ています。ASP.NET MVC4ルーティングの問題

' 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} 
    ) 

答えて

7

順序は問題ありません。

あなたがsite.com/details/abc123をリクエストすると、それはあなたの最初のルートと一致すると思います。

あなたはあなたの製品コードがnullである理由である

controller = "details"

action = "Index"

genre = "abc123"

を取得します。

2つのroute.MapRoute文を前後に切り替えると、問題が解決するはずです。

あなたの2番目のルートはindexではなく、infoに設定されていますが、それは誤植であると仮定していますか?