Attribute Routing and Parameters in MVC5

I assume you are familiar with MVC5 and its various features. One of the interesting features of MVC5 is attribute routing. We understand routing in MVC; we can say it is the most important feature of the Model, View and Controller design pattern. It shows the real beauty of separation of concerns. In all previous versions of MVC, we saw that the routing mechanism is defined in one place, basically it's in the “RouteConfig.cs” file.

It's great to keep all the routing information in one place to make it centralized. But the problem is that if we declare a routing mechanism in this way, then all the actions and the controller will follow the same routing technique. Now if we wanted to define a different routing to a different controller then we need to implement attribute routing.

Using attribute routing we can define various routing techniques for different controllers, so the user will get more freedom to use the application.

Since this is called attribute routing, we will use the Route() attribute to the action. In this example we defined two actions and we have specified a route above this.

The first products() action will execute when we browse to the following URL.

http://localhost:2968/products/shirt

And if we want to invoke a second action then we need to browse to the following URL.

http://localhost:2968/products/shirt/10

Now, to enable attribute routing we need to enable it in the Route.Config.cs file. Just add the following single line of code.

enable attribute routing

Here is sample code to execute.

using System;

using System.Collections.Generic;

using System.Diagnostics;

using System.Linq;

using System.Web;

using System.Web.Mvc;

using MVC_5.App_Data;

 

namespace MVC_5.Controllers

{

    public class testController : Controller

    {

        [Route("products/shirt")]

        public void products()

        {

        }

        [Route("products/shirt/{id}")]

        public void products(int id)

        {

        }

    }

}

Here is sample output of when we are browsing.

http://localhost:2968/products/shirt/10

And we are seeing that it has stuck to the second action.

action

Pass optional parameter

We can combine two actions into one if we make the parameter as an optional parameter. Here is sample code.

public class testController : Controller

{

    [Route("products/shirt/{id?}")]

    public void products(int? id)

    {

    }

}

Now, this action will get everything for the preceding URLs.

http://localhost:2968/products/shirt
http://localhost:2968/products/shirt/10

Define Route constraints

We can specify parameters constraints placing the constraint name after the parameter name separated by a colon.

[Route("products/shirt/{id:int}")]

 public void products(int id)

 {

 }

In the example above we have defined id as an integer type. It needs to be able to define any data type. Here I have given a few examples.

x:bool

Matches a bool parameter.

x:maxlength(n)

Matches a string parameter with a maximum length of n characters.

x:minlength(n)

Matches a string parameter with a minimum length of n characters

x:max

Matches an integer parameter with a maximum value of n.

x:min

Matches an integer parameter with a minimum value of n.

x:range

Matches an integer parameter within a range of values.

x:float

Matches a floating-point parameter.

x:alpha

Matches uppercase or lowercase characters.

x:regex

Matches a regular expression.

x:datetime

Matches a DateTime parameter.

Conclusion

We have seen how attribute routing works in MVC 5. Attribute routing is very useful when the routing mechanism is not fixed for all controllers.

Up Next
    Ebook Download
    View all
    Learn
    View all