ASP.NET MVC Controller Attribute Route Precedence

 Author: Shengtao Zhou       Created: 2/15/2019 4:34:13 AM       Modified: 2/15/2019 7:51:28 PM   More...
It's very easy to get lost for MVC routing, it's a good idea to brush up on it.

This is the default route declaration in ASP.Net MVC,
    public class MvcApplication : System.Web.HttpApplication
    {
        public static void RegisterRoutes(RouteCollection routes)
        {
            routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
            routes.MapRoute(
                "Default",              //Name                                
                "{controller}/{action}/{id}",                    //Pattern        
                new { controller = "Home", action = "Index", id = "" }  //Handler
            );
        }
        protected void Application_Start()
        {
            RegisterRoutes(RouteTable.Routes);
        }
    }

MVC 5 supports a new type of routing called attribute routing. In earlier verison of MVC, you need to use third party routing to achieve this.

Check more details in Attribute Routing in ASP.NET Web API 2.

It decorates a controller action with a [Route] attribute. To enable attribute routing, call MapHttpAttributeRoutes in Register method.

    public static class WebApiConfig
    {
        public static void Register(HttpConfiguration config)
        {
            // Web API routes
            config.MapHttpAttributeRoutes();
            // Other Web API configuration not shown.
        }
    }

A request could match multiple route patterns. The most specific routes have a chance to execute before the more general routes. It only take the first match using the following Precedence.

1. First, check the Order property.

An Order attribute can be added to a route like "Order=1". By default the order is 0. Routes are processed according to an ascending sort of the Order property.

    [Route("posts", RouteOrder = 1)]
    public ActionResult GetData() { 
        ... 
    }

By default, all defined routes have an Order value of 0 and routes are processed from lowest to highest. 

2. Then check Routes without explicit Order attribute as following orders  

a) Literal segments

Hard corded route like this,
    [Route("posts/software")]
    public ActionResult GetData() { ... }

b) Route parameters with constraints, which include parameter place holders
    [Route("posts/{categoryId:int}")]
    public ActionResult GetData() { ... }

c) Route Parameters without constraints
    [Route("posts/{category}")]
    public ActionResult GetData() { ... }

d) Wildcard parameters with constraints. (A wildcard parameter can match multiple parameters)
    [Route("posts/{*categoryId:int}")]
    public ActionResult GetData() { ... }

e) Wildcard parameters without constraints
    [Route("posts/{*categoryId}")]
    public ActionResult GetData() { ... }

3. Routing matching uses case in-sensitive comparison

More...          Back to List          

(Please enter you comments between 100 to 2000 characters. Thanks for your contribution.) 

         Created:       Modified: 

Editing a comment

       (Please enter you comments between 100 to 2000 characters. Please login before edit comment.)