Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Multiple RoutePrefixes per controller using MVC Attribute routing?

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?

like image 311
Matthew Verstraete Avatar asked Jun 29 '14 01:06

Matthew Verstraete


People also ask

Can we have multiple routing in MVC?

Multiple Routes You need to provide at least two parameters in MapRoute, route name, and URL pattern. The Defaults parameter is optional. You can register multiple custom routes with different names.

Can we define multiple routes for single action?

Yes, We can use multiple URLs to the same action with the use of a routing table. foreach(string url in urls)routes. MapRoute("RouteName-" + url, url, new { controller = "Page", action = "Index" });

How can we set attribute routing in MVC?

Enabling Attribute Routing in ASP.NET MVC Enabling attribute routing in your ASP.NET MVC5 application is simple, just add a call to routes. MapMvcAttributeRoutes() method with in RegisterRoutes() method of RouteConfig. cs file. You can also combine attribute routing with convention-based routing.

How many routes can be defined in the MVC application?

there are no limits in creating routes. You can create as many route as you want in your RouteConfig. cs file. But make sure that you provide unique name value to each MapRoute function.


1 Answers

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/")]

like image 60
Matthew Verstraete Avatar answered Sep 18 '22 18:09

Matthew Verstraete