2017-05-25 95 views
0

我必須能夠應對這樣的路線: MyController/ElementType 出於這個目的,我創建瞭如自定義路線這樣的:微分MVC路由爲{}控制器/參數{控制器}/{行動} PARAM = myvalue的

context.MapRoute(
        "NameOfTheRoute", 
        "MyPath/{controller}/{elementType}", 
        new { controller = "Elements", action = "Create" } 
       ); 

,它工作正常,問題是,當我有這樣 /MyPath/Elements/GetElementType?elementType=fire88

GetElementType的路線是不同的動作,但它進入Create行動,因爲我之前宣佈自定義路線,我該如何讓知道r外出他們是不同的行動?

回答

1

爲什麼要這條路線,因爲你沒有定義route處理action所以MyPath/{controller}/{elementType}意味着後controller一切的名稱將被視爲{elementType}所以 你必須創建另一個將處理action另一條路線的原因

routes.MapRoute(
    "MyPathRouteWithAction", 
    "MyPath/{controller}/{action}/{elementType}", 
    new {controller = "Elements", action = "Create"} 
); 

routes.MapRoute(
    "NameOfTheRoute", 
    "MyPath/{controller}/{elementType}", 
    new {controller = "Elements", action = "Create"} 
); 

第一custom route將處理routs/MyPath/Elements/GetElementType/fire88在我的情況

+0

的網址是不是'/ mypath中/元/ GetElementType/fire88',我就像'/ MyPath/Elements/GetElementType?elementType = fire88'。我現在所做的是創建一個鏈接,執行如下操作:'link 1'並修改'default'路由,只是將'id'改爲'elementType',現在它正在工作,但我不是確定這是最好的方式... – AlexGH

+0

@AlexGH你應該使用'link 1' ' – Usman

+0

我想這樣第一:link 1,但我得到這樣的網址:'/ MyPath/Elements/GetElementType?elementType = fire88'我可能沒有正確更新瀏覽器,因爲我現在再試一次,正在這樣工作,謝謝 – AlexGH