Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MVC 5 AttributeRouting Catch All

How do I create a catch all route with the new Attribute routing in MVC

I tried this: [Route("{pagenode}", Order = 999)]

But when I have a named route like [Route("contact"]

I get the "Multiple controller types were found that match the URL. This can happen if attribute routes on multiple controllers match the requested URL." error.

like image 955
Ivo Avatar asked Mar 20 '14 16:03

Ivo


People also ask

Can you enable attribute routing in MVC 5?

MVC 5 supports a new type of routing, called attribute routing. As the name implies, attribute routing uses attributes to define routes. Attribute routing gives you more control over the URIs in your web application. The earlier style of routing, called convention-based routing, is still fully supported.

Which ASP.NET MVC 5 offers attribute based routing?

ASP.NET MVC5 and WEB API 2 supports a new type of routing, called attribute routing. In this routing, attributes are used to define routes. Attribute routing provides you more control over the URIs by defining routes directly on actions and controllers in your ASP.NET MVC application and WEB API.

What is routing in MVC 5 with example?

Routing is a pattern matching system. Routing maps an incoming request (from the browser) to particular resources (controller & action method). This means routing provides the functionality to define a URL pattern that will handle the request. That is how the application matches a URI to an action.

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.


1 Answers

This can be done with Attribute Routing if the first "directory" in the path is fixed.

For example, to match anything that hits /questions or /questions/4 or /questions/answers/42 then you would use [Route("questions/{*catchall}"].

like image 176
Jim Buck Avatar answered Oct 15 '22 21:10

Jim Buck