2017-05-03 10 views
0

製品を表示するためのProductControllerがあります。 製品にはスラッグとカテゴリがあります。ASP.NET MVCルート:製品カテゴリとスラッグ

私は、次のかなりのURL(例)好ましくルート属性[HttpGet("route-here")]として実装

URL            CONTROLLER ACTION     PARAMETER 
www.example.com/products     -> ProductController.Products  : "" (empty string) 
www.example.com/products/phones   -> ProductController.Products  : "phones" 
www.example.com/products/phones/iPhone  -> ProductController.Details  : "phones" &"iPhone" 

をしたいです。しかしroutes.MapRouteStartup.csでもOKです。

ありがとうございました。

[HttpGet("products/{category?}")] 
public ActionResult Products(string category) 

しかし、最後のルートははないが動作するように見えるん:

[HttpGet("products/{category}/{slug}")] 
public ActionResult Details(string category, string slug) 
+1

*何が問題ですか?あなたは何を試しましたか?* –

答えて

1

私ができる私は 最初の2つのルートは、現在、私のために働いている試みた何

あなた自身で解決しようとせずにこれに答えたと信じていませんが、コードはここにあります

public class ProductController : Controller 
{ 
    [Route("products/{productType}")] 
    public ActionResult Products(string productType) 
    { 

    } 

    [Route("products/{productType}/{product}")] 
    public ActionResult Details(string productType, string product) 
    { 

    } 
} 
+0

私の更新された質問に見られるように、私はすでにこれを試しています。しかし、私はばかです。私はちょうど欠けているいくつかを監督していた。ToLower()。私の悪い。あなたの答えは正しいです。 – capcapdk

関連する問題