Attribute Routing In ASP.NET Core

Attribute Routing In ASP.NET Core

·

3 min read

Table of contents

Intro:-

ASPNET.Core have two types of Routing: Conventional Routing and Attribute Routing. For web app, conventional routing is desired, whereas for Rest API, attribute routing is used. Conventional routing's template is provided by default in Program.cs file(.net6) and Startup.cs(.net3.1), whereas to add attribute routes, you have to add app.MapControllers(); in Program.cs file(.net 6).

routing.jpg

  • Attribute routing allows you to define routes right next to the actions you map to.

  • All routes are evaluated at the same time, in order to find the best match. Because of this, attribute routing wants you to be very specific when defining routes and route patterns.

  • Attribute routes are defined in the controller classes as attributes on actions.

  • Attribute routing can be applied to both the action and the controller.

  • Attribute routing, in general, have a single route per action, where each route uniquely identifies an action

  • Attribute routing is used for specialized actions (e.g. ones with multiple parameters, or unique route segments, etc.)

  • attribute routing creates a tree of all routes and evaluates them sequentially with priority basis,some routes will always be evaluated earlier than least-specific ones.

Let's say we have the following attributes:

[Route("/post/{purchase}/{id}")]

and

[Route("/post/{**article}")]

In attribute routing, the first route will always be evaluated before the second, because it is more specific .

  • REST APIs generally use attribute routing for mapping the app's functionality as a collection of resources and executions are denoted by HTTP verbs.

  • Attribute routing uses a some set of attributes to map actions directly to route templates.

  • Attribute routing requires more data to specify a route.
 using  Microsoft.AspNetCore.Mvc;
using System.Security.Cryptography.X509Certificates;

var builder = WebApplication.CreateBuilder();
builder.Services.AddControllersWithViews();

var app = builder.Build();
app.MapControllers();

app.Run();
[Route("")]
[Route("Home")]
[Route("Home/Index")]
public class HomeController : Controller
{
    public ActionResult Index()
    {
        return new ContentResult
        {
            Content = @"
            <html><body>
            <b>Hello world</b>
            <br/></br>
            The following links same controller and action.
            <ul>
               <li><a href =""/"">/</a></li>
               <li><a herf =""home"">/home</a></li>
               <li><a herf =""home/index"">/home/index</a></li>
            </ul>
            </html></body>",
            ContentType = "text/html"

        };

    }

    [Route("About")]
    public ActionResult About()
    {
        return new ContentResult
        {
            Content = @"
                <html><body>
                <b>About Page</b>
                <br/><br/>
                <ul>
                    <li><a href=""/about"">/about</a></li>
                </ul>
                </body></html>",
            ContentType = "text/html"
        };
    }

}

Suppose you have three attribute routes, on HomeController:-

[Route("")]
[Route("Home")]
[Route("Home/Index")]
public class HomeController : Controller

attribute_routing_1.png

attribute_routing_2.png

then all routes(localhost:port_number/Home and localhost:port_number/Home/Index) points to the same route, which is home page.

Attribute routes can be applied on action method also, suppose you have"about" route, applied on about()action method, then it will display
->localhost:port_number/about page:

[Route("About")] public ActionResult About()

attribute_routing_3.png

Conclusion:-

Attribute Routing is like attribute feature of C#, its meta routing,
can be applied on controllers and action methods, multiple attribute routing can be applied on controller level, however only one attribute route can be applied on action method.