Learn Attribute Routing in ASP.NET Core

Learn Attribute Routing in ASP.NET Core

So far what you have seen is Convention Routing. There is another technique of routing which is known by the name of Attribute Routing. In Attribute Routing you apply route as C# attributes directly to the controller and actions.

Let’s take a simple example first. Apply the attribute – [Route(“CallRoute”)] to the Home Controller’s Index Action method so that it looks like:

[Route("CallRoute")]
public IActionResult Index()
{
    return View();
}

Remove all the Convention Routes except the default one:

app.UseMvc(routes =>
{
    //Default Route
    routes.MapRoute(
        name: "default",
        template: "{controller=Home}/{action=Index}/{id?}");
});

Now run your application and go to the URL – ‘/CallRoute’. You will see the Index action method of the Home controller is invoked.

You will also note that your previous URL of this Index action – ‘/Home/Index’ no longer works as it prevents the routes from Convention Routing to be applied.

However other action routes from Convention routing like – ‘Home/Check/Ram’ will continue to work. So that means – If there is an Attribute Routing applied to a Controller or an Action then the Convention Routes for the Controller or Action will not work.

The ‘controller’ & ‘action’ terms in Attribute Routing

The [controller] and [action] terms are used to reference the Controller and Action names. This is just like Convention Routing. To understand it create a new Controller and name it Admin. Add the following code to this controller:

using Microsoft.AspNetCore.Mvc;
namespace URLRouting.Controllers
{
    public class AdminController : Controller
    {
        [Route("[controller]/CallMe")]
        public string Index()
        {
            return "'Admin' Controller, 'Index' View";
        }

        public string List()
        {
            return "'Admin' Controller, 'List' View";
        }
    }
}

You can see that I applied the attribute route to the Index action method and that has the term ‘controller’ which states that the URL should have the Controller segment in it.

So to invoke the Index method you have to go to the URL – ‘/Admin/CallMe’.

However there is no Attribute Routing applied to the List action so its URL will be formed by the Convention Routing – ‘Admin/List’.

To see how the action term is applied, add another action method with attribute routing as shown below:

[Route("[controller]/CallMe/[action]")]
public string Show()
{
    return "'Admin' Controller, 'Show' View";
}

To invoke this action the URL will be – ‘/Admin/CallMe/Show’

Attribute Routing applied on the Controller

You can also apply attribute routing on the Controller. This will make all its action method inherit it. Change the code of the Admin Controller as shown below:

using Microsoft.AspNetCore.Mvc;

namespace URLRouting.Controllers
{
    [Route("News/[controller]/USA/[action]/{id?}")]
    public class AdminController : Controller
    {
        public string Index()
        {
            return "'Admin' Controller, 'Index' View";
        }

        public string List(string id)
        {
            return "'Admin' Controller, 'List' View";
        }

    }
}

The Attribute Routing is applied to the Admin controller so both its action method inherit it.

Here the URLs to invoke Index action will be:

1. /News/Admin/USA/Index
2. /News/Admin/USA/Index/100
3. /News/Admin/USA/Index/Hello

And the URLs to invoke List action will be:

1. /News/Admin/USA/List
2. /News/Admin/USA/List/100
3. /News/Admin/USA/List/Hello

Route Constraint in Attribute Routing

Route Constraint can be applied in the Attribute Routing. I had created ‘OnlyGodsConstraint’ earlier, it can be applied to the attribute routing in the Admin controller just like the below code:

using Microsoft.AspNetCore.Mvc;

namespace URLRouting.Controllers
{
    [Route("News/[controller]/USA/[action]/{id:allowedgods?}")]
    public class AdminController : Controller
    {
        public string Index()
        {
            return "'Admin' Controller, 'Index' View";
        }

        public string List(string id)
        {
            return "'Admin' Controller, 'List' View";
        }

    }
}

So the URL to invoke the Index method will be – ‘/News/Admin/USA/Index/Shiv’ as shown in the below image:

attribute routing constraint

You can download the full codes of this tutorial from the below link:

Download

Conclusion

In this tutorial you learned to use Attribute-Based Routing. I hoped you liked this tutorial. Look out for other tutorials on ASP.NET Core.

Next tutorial – ASP.NET Core Routing: Generating Outgoing URLs in Views.

Share this article -

yogihosting

ABOUT THE AUTHOR

This article has been written by the Technical Staff of YogiHosting. Check out other articles on "WordPress, SEO, jQuery, HTML" and more.