Multiple RoutePrefixes per controller using MVC Attribute routing?

[亡魂溺海] 提交于 2019-11-29 20:07:52

问题


With the new MVC Attribute routing, I know you can assign multiple Route attributes to a single ActionResult, but I am looking for a way to do the same at the RoutePrefix level. I have a controller which, in every action, should be accessible by three routes:

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

I tried putting three individual RoutePrefix attributes, but I get the error Deuplace RoutePrefix attribute. If I try to use a comma-separated list, I get Best override method for does not contain a constructor that takes 3 arguments.

Is it possible to set up a RoutePrefix so that it takes the three routes I want for my controller?


回答1:


Running a bunch of tests I found out that I can just add 3 Route attributes to the controller level and it works the way I want.

Edit: a better way to do it I found was using the regex match method

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



来源:https://stackoverflow.com/questions/24472300/multiple-routeprefixes-per-controller-using-mvc-attribute-routing

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!