每个控制器使用MVC属性路由有多个RoutePrefix? [英] Multiple RoutePrefixes per controller using MVC Attribute routing?
问题描述
借助新的MVC属性路由,我知道您可以将多个Route
属性分配给单个ActionResult
,但是我正在寻找一种在RoutePrefix
级别执行相同操作的方法.我有一个控制器,在每一个动作中,应该可以通过以下三种途径来访问它:
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}
/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
.
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
.
是否可以设置RoutePrefix
,使其采用我想要的控制器的三条路线?
Is it possible to set up a RoutePrefix
so that it takes the three routes I want for my controller?
推荐答案
运行大量测试,发现可以将3个Route
属性添加到控制器级别,并且可以按照我想要的方式工作.
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.
我发现的一种更好的方法是使用正则表达式匹配方法
a better way to do it I found was using the regex match method
[RoutePrefix("{Type:regex(Games|Cinema|Books)}/{SectionRoute}/Character/")]
这篇关于每个控制器使用MVC属性路由有多个RoutePrefix?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!