每个控制器使用 MVC 属性路由的多个 RoutePrefixes? [英] 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}
我尝试放置三个单独的 RoutePrefix
属性,但出现错误 Deuplace RoutePrefix 属性
.如果我尝试使用逗号分隔的列表,我会得到 不包含带 3 个参数的构造函数的最佳覆盖方法
.
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 属性路由的多个 RoutePrefixes?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!