Routing is the backbone of the .NET MVC web application, determining how URLs map to controller actions. In this article, we will explore the different routing approaches available, from default routing to more advanced attribute-based routing, equipping you with the knowledge to create intuitive and flexible URL structures.
Previous Chapter: Temp Data, View Bag, and View Data in .NET MVC
Convention-based
The default routing in .NET MVC follows a convention-based approach, where the URL structure maps directly to the controller and action names.
URL Pattern
Conventional routing follows a specific URL pattern, such as:
{controller}/{action}/{id}
Define the default Route
.NET MVC provides a MapRoute
method to define the default route.
// Define the default route
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
Here, the name of the route is Default
. The URL pattern is {controller}/{action}/{id}
which means the URL contains the controller name, action name and an optional parameter.
Route Generation
The routing engine automatically generates the appropriate URL based on the controller and action names.
Attribute Routing
Attribute routing allows you to define your routes directly on the controller and action methods. This approach gives you more granular control over your URL structure and routing logic. Attribute routing enables the creation of more meaningful and intuitive URLs for your web application.
Defining Routes with Attributes
Let’s take a look at defining attribute routing in .NET MVC:
[Route("sports/home")]
public async Task<IActionResult> Index(int pageNumber = 1, int pageSize = 25)
{
var articles = await _pilotService.SportsPageContent(2, pageNumber, pageSize);
return View(articles);
}
In this example, the routes would be:
/sports/home for Index action.
Route Parameters
Attribute routing supports URL parameters, allowing you to extract values from the URL and pass them to the action method.
[Route("article/{id}")]
public async Task<IActionResult> Index(string id)
{
var articles = await _pilotService.LoadContent(id);
return View(articles);
}
Here, {id}
is the parameter of the web page URL.
You may like this article: Build Your First MVC Application in Visual Studio 2022
Route Constraints in .NET MVC
MVC provides Route Constraints to restrict to match a route for a given URL on the basis of route parameters.
Basic Route Constraints
In this example, we will create routes with constraints on the parameter values.
[Route("products/details/{id:int}")]
public ActionResult Details(int id)
{
return View();
}
Types of Route Constraints in .NET MVC
Regex Constraints
Regular expression constraints allow you to enforce specific patterns for route parameters.
Data Type Constraints
You can constrain route parameters to specific data types, such as int, decimal, or guide.
Custom Constraints
Developers can create their own custom route constraints to enforce complex business rules.
Conclusion and Best Practices
- Default Routing: Use for simple, convention-based URL structures
- Attribute Routing: Leverage for more complex, customized URL requirements
- Routing Constraints: Implement to enforce business rules and maintain data integrity
Next Chapter: .NET MVC with SQL Server Database