What is routing? Explain how it works in ASP.NET Core

ASP.NetServer Side ProgrammingProgramming

In the context of web application frameworks, routing matches an incoming HTTP request to executable code. The executable code works as an endpoint that handles the request and returns a response.

ASP.NET Core defines and configures the endpoints when the application starts. Routing also handles extracting the values from the request, building appropriate objects, and passing them to the methods that handle the request.

Routing has been an important part of ASP.NET Core from the beginning. However, ASP.NET Core 3.0 introduced a new routing system called endpoint routing. It decouples routing from the MVC framework and makes it a more fundamental feature of ASP.NET Core. You can still use it to configure MVC routes, but in addition, the new routing system allows you to configure the routing using:

  • Controllers

  • Razor Pages

  • SignalR

  • gRPC Services

  • Delegates and lambdas

You can also use routing in ASP.NET Core to map requests with multiple URLs to the same controllers or Razor Pages.

When you generate a new ASP.NET Core application using the templates, it includes routing in the generated code. You register routing in the middleware pipeline, which is defined in the Configure() method on the Startup class. In the example below, the bolded code configures the routing for your application.

public void Configure(IApplicationBuilder app, IWebHostEnvironment env){
   if (env.IsDevelopment()){
      app.UseDeveloperExceptionPage();
   }else{
      app.UseExceptionHandler("/Error");
      // The default HSTS value is 30 days. You may want to change this for production
      app.UseHsts();
   }
   app.UseHttpsRedirection();
   app.UseStaticFiles();
   app.UseRouting();
   app.UseAuthorization();
   app.UseEndpoints(endpoints =>{
      endpoints.MapRazorPages();
   });
}
  • UseRouting() method adds route matching to the middleware pipeline, which selects the best match for the request after inspecting the defined endpoints.

  • UseEndPoints() adds endpoint execution to the middleware pipeline.

You can configure routing using two different ways:

  • Convention-based routing

  • Attribute-based routing

Which routing strategy you choose depends upon which type of web application you are building, i.e., MVC or Razor Pages, and whether you are building an API or a web application with the user interface.

Convention-based routing applies globally for your application, i.e., all the MVC controllers or Razor Pages can use it. You define your controllers, methods, or Razor Pages using well-defined ASP.NET Core conventions to map the requests to endpoints. Though this approach works well for most cases, it makes it challenging to create custom routes and endpoints.

Using attribute-based routing, you can use the C# attributes such as [Route] on the controllers to map a given URL to a specific endpoint. Attribute-based routing provides more flexibility in terms of giving custom names to the routes and matching a request to the endpoints. You can explicitly define what the URL for each endpoint should be. Though it can be verbose, the additional flexibility can prove useful.

raja
Published on 22-Jun-2021 14:21:21
Advertisements