Attribute Routing in ASP.NET MVC

With earlier version of ASP.NET MVC routing was strictly convention based and there was no way to give custom routes for specific controller and action associated with it. With ASP.NET MVC 5 and above attribute based routing feature was introduced. 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 supported and you can combine both techniques in the same controller and web application as well. To enable attribute routing, call MapMvcAttributeRoutes during configuration in RouteConfig.cs file

public static void RegisterRoutes(RouteCollection routes)
{
    routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

    routes.MapMvcAttributeRoutes();

    routes.MapRoute(
        name: "Default",
        url: "{controller}/{action}/{id}",
        defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
    );
}

Now in the controller as part of action we can declare custom routes for each and every action

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

    [Route("users/about")]
    public ActionResult About()
    {
        ViewBag.Message = "Your application description page.";
        return View();
    }

    public ActionResult Contact()
    {
        ViewBag.Message = "Your contact page.";
        return View();
    }
}

You can set a common prefix for an entire controller by using the [RoutePrefix] attribute like show below

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

    [Route("users/about")]
    public ActionResult About()
    {
        ViewBag.Message = "Your application description page.";
        return View();
    }

    public ActionResult Contact()
    {
        ViewBag.Message = "Your contact page.";
        return View();
    }
}

When to use Attribute Routing
The convention-based routing is complex to support certain URI patterns that are common in RESTful APIs. But by using attribute routing you can define these URI patterns very easily. For example, resources often contain child resources like Clients have orders, movies have actors, books have authors and so on. It’s natural to create URIs that reflects these relations like as: /clients/1/orders. This type of URI is difficult to create using convention-based routing. Although it can be done, the results don’t scale well if you have many controllers or resource types. With attribute routing, it’s pretty much easy to define a route for this URI.

Similar attribute routing concept holds good for Web API 2

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s