Thursday, February 26, 2015

Attribute Routing in MVC 5

In the previous blog post, I talked about traditional routing. And as promised, today I am going to talk about the new feature in routing introduced in MVC 5 called Attribute Routing.

Attribute Routing

This is a way to add routes using attributes at the controller or action method level. This is quite simpler as compared to traditional routing as route URLs are kept together with the controller code. Both traditional routes and attribute routes handle complex scenarios and can go hand in hand with each other. Personally, I like it better than traditional routing because it gives me better control.

Let's take a look at an example. In order to use this feature, we will first need to add this code to our RegisterRoutes() method.

public static void RegisterRoutes(RouteCollection routes)
{
     routes.MapMvcAttributeRoutes();
}


This maps the attribute defined routes in the application.
Next, we define the route at the action method.

public class HomeController : Controller
{
        [Route("")]
        [Route("Home")]
        [Route("Home/Index")]
        public ActionResult Index()
        {
            return View();
        }
}


So, this makes the action method accessible by either of the three URLs:
i) /
ii) /Home
iii) /Home/Index

Note that we can define multiple routes simultaneously for the same action method using multiple route attributes. The string passed to the route attribute is called route template. The route template is basically a pattern matching rule that determines whether the route applies to an incoming request.

Parameter in Attribute Route

Next, let's take a look at how to pass some values in our route.

[Route("Student/{id}")]
public ActionResult Details(int id)
{
       //use the id here
}


Upon parsing the request URL, routing places the route parameter value into RouteValueDictionary which can be accessed via RequestContext. The dictionary uses route parameter names as the keys and corresponding subsections of the URL as values in the dictionary.

Controller Level Attribute Route

Similar to action method we can also put attribute route at the controller level itself. If all the action methods have similar route structure then its easier to put it at the controller level. Let's take a look at an example:

[Route("Home/{action}")]
public class HomeController : Controller
{
        public ActionResult Index()
        {
            return View();
        }

        public ActionResult About()
        {
            return View();
        }

        public ActionResult Contact()
        {
            return View();
        }

} 


This is equivalent to putting separate routes on each action. So it's lesser effort on our part :)
Similar to action methods, we can also have multiple attribute routes at the controller level too.

Conclusion

Overall, since it's new, attribute routing is a lesser known feature of MVC5. I like this feature a lot as I can look at the routes right on top of my action methods which in turn, is more clear and reduces the guess work.
This gives us control on routing at action level or controller level. Traditional routing is more at application level. Usually a mix and match approach is pretty good depending on the application needs.

For future updates to my weekly blog, please subscribe to the blog using your email id (at the right) and follow me on Twitter. Until then Happy Coding :)


No comments:

Post a Comment