Airo Global Software

Think Beyond Future !

What is Attribute-Based Routing In ASP.NET MVC 5?

- Posted in Web Development by

enter image description here

We all understand that ASP.NET MVC is a great stage that allows us to build and maintain web applications in a much easier manner related to form-based web applications. There are a few things in MVC-based web applications that run a little more uncomfortably than standard web applications, one of them is routing.

Until now, there has been a routing table that you can set either in the Global. asax or in the RouteConfig.cs and all incoming calls would look it up to determine the rendering of a targeted view.

Here is the code that you might have seen previously to have note-variable routes in MVC 4 in the following example of the Route collection object.

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

What is the necessity for this new routing methodology?

There was nothing really incorrect with the previous method of routing and in fact, you can however use it in MVC 5 or use this new routing method in connection with the old one.

Here are a few benefits of attribute-based routing,

  • Helps developers in the debugging / troubleshooting form by providing data about routes.
  • Decreases the chances for mistakes, if a route is changed incorrectly in RouteConfig.cs then it may affect the whole application's routing.
  • May decouple controller and business names from route entirely.
  • Simple to map two routes guiding to the same action.

Let's see specifically how we can configure it and see it working.

First, we will need to enable attribute-based routing on our MVC web application that can be made by only one line. All you want to do is put this line in the RegisterRoutes method of the application.

public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
tes.MapMvcAttributeRoutes(); //Enables Attribute Based Routing
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Product", action = "List", id = UrlParameter.Optional }
);
}

Now, here is how you can use attribute-based routing on a particular action method.

[Route("products/{id?}")]
public ActionResult Details(string id)
{
if (string.IsNullOrEmpty(id))
{
return View("List", GetProductList());
}
return View("Details", GetProductDetails());
}

As shown in the method above, the Route is determined on a Details action method that lets users enter the product specifications page either by of these paths,

/Product/Details/Id or /products/id

You might have recognized the problem mark in the route above, all it means is that the id is an arbitrary parameter of the route and hence the business process logic checks if the id is null. It will show you the result listing page.

Route Prefixes

Route Prefixes are blank but the prefix for any route that we need to apply, all we need to do is to determine the route prefix on a controller so that all the action plans inside it can follow the prefix.

[RoutePrefix("products")]
public class ProductController : Controller  
{
//This will be translated to /products
[Route]
public ActionResult List()
{
return View();
}
//This will be translated to /products/2
[Route("{id?}")]
public ActionResult Details(string id)
{
if (string.IsNullOrEmpty(id))
{
return View("List");
}
return View("Details");
}
}

Route Constraints

Route constraints are zero but a set of practices that you can set on your routing model/parameters that users want to follow when entering the established routes.

The way to determine a constraint is by using the ":" character, let's have a look at the example below.

//route gets called as /products/productname
[Route("products/{id:alpha}")]
public ActionResult GetProduct(string name) 
{
return View();
}
//route gets called as /products/2  
[Route("products/{id:int}")]
 public ActionResult GetProduct(int id)
{
return View();
}

Now your strength has recognized in the example above that though the system name is equal the route's parameter has some restrictions on it. In other words, the first order will be called if the route is located with a string as a parameter and the second method will be called if the route is obtained with an integer in the route parameter.

You can also define your system route limitations using an IRouteConstraint interface.

Route Areas

A Route area is zero but the way to define the area in a route, essentially just to let the route know that the controller goes to some area.

[RouteArea("business")]
[RoutePrefix("products")]
public class ProductController : Controller
{
//This will be translated to /business/products/list 
[Route]
public ActionResult List()
{
return View();
}
}

If you have any questions about the above topic or have to get services and consultations and get the best ASP.Net MVC Application Development Services. Feel free to contact us. AIRO GLOBAL SOFTWARE will be your digital solution. E-mail id: [email protected]

enter image description here

Author - Johnson Augustine
Chief Technical Director and Programmer
Founder: Airo Global Software Inc
LinkedIn Profile: www.linkedin.com/in/johnsontaugustine/