aspnet-mvc
  1. aspnet-mvc-attribute-routing

Attribute Routing - (ASP.NET MVC Routing)

ASP.NET MVC Routing is responsible for mapping incoming URLs to specific controller actions. In this tutorial, we'll discuss Attribute Routing, which allows you to specify the routing rules for your application at the controller or action level using attributes.

Syntax

The syntax for defining Attribute Routing in ASP.NET MVC is as follows:

[Route("{controller}/{action}/{id}")]
public ActionResult Index(int id)
{
    // Action logic here
}

In this example, we're using the [Route] attribute to define the routing rule for the Index() action. We're specifying three placeholders (controller, action, and id) as part of the URL.

Example

Let's look at an example of how to use Attribute Routing in ASP.NET MVC.

public class HomeController : Controller
{
    [Route("/")]
    [Route("Home/Index")]
    public ActionResult Index()
    {
        ViewBag.Message = "Welcome to my website!";

        return View();
    }

    [Route("Home/{category}")]
    public ActionResult Category(string category)
    {
        ViewBag.Message = $"You have selected the {category} category.";

        return View();
    }
}

In this example, we have a HomeController with two actions, Index() and Category(). We're using the [Route] attribute to specify the routing rules for each action. The Index() action is mapped to the root URL ("/") and the "Home/Index" URL. The Category() action is mapped to any URL that starts with "Home/" followed by a category.

Explanation

Attribute Routing allows you to define routing rules for your application at the controller or action level using attributes. It provides a more flexible and fine-grained approach to routing compared to the traditional approach of defining routes in a central location in your application.

In the example above, we're using the [Route] attribute to define the routing rules for the HomeController. The Index() action is mapped to two different URLs, while the Category() action is mapped to any URL that matches the pattern "Home/{category}".

Use

Attribute Routing can be used to define custom routing rules for your application at the controller or action level. It allows you to create clean and user-friendly URLs that are easy to remember and recognize.

Important Points

Here are some important points to keep in mind when using Attribute Routing in ASP.NET MVC:

  • Attribute Routing provides a more flexible and fine-grained approach to routing compared to the traditional approach of defining routes in a central location.
  • You can use different routing attributes to define different patterns for different controller actions.
  • Attribute Routing is supported in ASP.NET Core MVC as well.

Summary

In this tutorial, we discussed Attribute Routing in ASP.NET MVC, which allows you to specify the routing rules for your application at the controller or action level using attributes. We covered the syntax, example, explanation, use, and important points of Attribute Routing. With this knowledge, you can use Attribute Routing to define custom routing rules for your application and create clean and user-friendly URLs.

Published on: