簡體   English   中英

將{Controller} /參數與{Controller} / {action}的MVC路由區分開來?param = myvalue

[英]Differentiate MVC routing for {Controller}/parameter to {Controller}/{action}?param=myvalue

我必須能夠處理這樣的路由: MyController/ElementType為此,我創建了一個這樣的自定義路由:

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

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

GetElementType是一個不同的動作,但是由於我之前聲明了自定義路由,因此轉到了Create動作,如何知道它們是不同的動作呢?

之所以選擇這條路線,是因為您未定義處理actionroute ,因此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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM