net-core
  1. net-core-routing

Routing in ASP.NET Core MVC

Routing in ASP.NET Core MVC refers to how URLs map to controllers and actions. Routing is an essential component of any web application as it provides users with an easy-to-use URL structure. The ASP.NET Core MVC framework supports a flexible and powerful routing system that enables developers to configure custom routes and constraints to map URLs to controllers.

Syntax

The basic syntax of a route in ASP.NET Core MVC is as follows:

[HTTP Verb]("[Route Template]", Name = "[Route Name]")

We use [HTTP Verb] to specify the HTTP verb type for the route, such as GET, POST, PUT, DELETE, etc. The [Route Template] indicates the route pattern and the [Route Name] provides a human-readable name for the route. Here is an example:

[HttpGet]
[Route("products/{id:int}")]
public IActionResult Get(int id) {
  // logic to retrieve a product from the database
  return Ok(product);
}

In the example above, we use the HttpGet attribute to specify that the route only handles the GET HTTP verb. The [Route] attribute specifies the URL pattern as products/{id:int}, where id is an integer value. The route maps to the Get action method in the controller, which retrieves a product from the database.

Example

Let's take a look at a more comprehensive example of how to use routing in ASP.NET Core MVC. Suppose we have a web application that handles customer orders. Our application has a set of products that customers can order, and we want to allow customers to place an order by navigating to the URL /order/{productId}/{quantity}.

We can define a controller with an action method that handles the order as follows:

public class OrderController : Controller {
  [HttpGet]
  [Route("order/{productId:int}/{quantity:int}")]
  public IActionResult PlaceOrder(int productId, int quantity) {
    // logic to place an order in the database
    return Ok("Order placed successfully");
  }
}

In the example above, we use the [HttpGet] attribute to specify that the route handles only the GET HTTP verb. The [Route] attribute specifies the URL pattern as order/{productId:int}/{quantity:int}, where productId and quantity are integer values.

Output

When the user navigates to the URL /order/{productId}/{quantity}, the ASP.NET Core MVC framework maps the URL to the PlaceOrder action method in the OrderController controller. The action method then processes the order and returns the result to the user.

Explanation

Routing in ASP.NET Core MVC works by matching the incoming URL to a controller and action method. The framework uses the route defined in the [Route] attribute to map the URL to the appropriate controller and action method. The route pattern can contain placeholders for parameters, which the framework extracts from the URL and passes as arguments to the action method.

Use

Routing in ASP.NET Core MVC is used to map incoming URLs to controller action methods. By using custom route patterns and constraints, developers can create easy-to-use and user-friendly URLs for their applications.

Important Points

  • Routing in ASP.NET Core MVC is essential for mapping URLs to controller action methods.
  • The basic syntax of a route in ASP.NET Core MVC includes the HTTP verb, route template, and route name.
  • The [Route] attribute in the controller provides the URL pattern for the route, and the route extracts parameter values from the URL.

Summary

Routing in ASP.NET Core MVC is an important feature of the framework, as it allows developers to map incoming URLs to controller action methods. By defining custom routes, developers can create user-friendly URLs and provide an intuitive experience for their users.

Published on: