Asp.net-Mvc-5

使用 MVC 屬性路由的每個控制器有多個 RoutePrefixes?

  • May 7, 2017

使用新的 MVC 屬性路由,我知道您可以將多個Route屬性分配給單個ActionResult,但我正在尋找一種在RoutePrefix級別上執行相同操作的方法。我有一個控制器,在每個操作中,都應該可以通過三種途徑訪問:

/Games/{Title}/Characters/{Route} /Books/{Title}/Characters/{Route} /Cinema/{Title}/Characters/{Route}

我嘗試放置三個單獨RoutePrefix的屬性,但我得到了錯誤Deuplace RoutePrefix attribute。如果我嘗試使用逗號分隔的列表,我會得到Best override method for does not contain a constructor that takes 3 arguments.

是否可以設置一個RoutePrefix以便為我的控制器採用我想要的三個路由?

執行一堆測試,我發現我可以Route在控制器級別添加 3 個屬性,並且它可以按照我想要的方式工作。

編輯:我發現一個更好的方法是使用正則表達式匹配方法

[RoutePrefix("{Type:regex(Games|Cinema|Books)}/{SectionRoute}/Character/")]

引用自:https://stackoverflow.com/questions/24472300