In this article, we will discuss about basic route concept in ASP.NET Core.
If you want to look at the previous articles of this series, please visit the links given below.
Routing functionality is responsible for mapping an incoming request to a route handler. Routes are defined in ASP.NET app and configured when the app starts. A route can optionally extract the values from the URL contained in the request and these values can then be used for the request processing. Using route information from ASP.NET app, the routing functionality is also able to generate URLs, which maps to route handlers. Therefore, routing can find a route handler based on a URL or the URL corresponding to a given route handler based on route handler information.
In Web development, routing serves to map all the incoming requests to the handlers and generate the URLs used in the responses. In ASP.NET Core, routing has been rewritten from the roots. Because routing with MVC and Web API was very similar, but both were using different frameworks (and code) to do the same thing, an important difference is that Web API supports RESTful routes, by default. For example, if a Controller’s action method name started with Post, then invoking an HTTP Post would call the method, by default. Since Microsoft decided to rebuild and unify the routing framework, what applies now for MVC, applies also for Web API. Before we dig into how to build routing, however, let’s review what are the advantages of routing for which it is important for our Application
Advantages of Routing
Routing basics
Routing uses routes (implementations of IRouter) to,
Generally, an app has a single collection of routes. When a request arrives, the route collection is processed in order. The incoming request looks for a route that matches the request URL by calling the RouteAsync method on each available route in the route collection. By contrast, a response can use routing to generate URLs (for example, for redirection or links) based on route information, and thus avoid having to hard-code URLs, which helps maintainability. Routing is connected to the middleware pipeline by the RouterMiddleware class. ASP.NET MVC adds routing to the middleware pipeline as part of its configuration.
Current ASP.NET Routing
Prior to going for further discussion about routing in ASP.NET Core, let’s remind ourselves about how route works in the ASP.NET 4.x.
Routing is integrated with the pipeline as an IHttpModule and when a route was resolved, it would bypass the rest of the pipeline and delegate to the final IHttpHandler through a new factory-type interface, the IRouteHandler.
It was through this IRouteHandler that MVC integrated with Routing, and this is important, because generally MVC-style URL's are extensionless, so the routing system enabled these types of URLs to be mapped to a specific IHttpHandler and in the case of MVC, this means mapping to the MVCHandler, which is the entry point for Controller/action execution. This means we didn't need to express a whole host of <httpHandler> rules for each unique route in our web.config file.
The MVC integration provided the MapRoute methods as extensions for a RouteCollection. Each route instance provides a Handler property - which by default is set to the MVCRouteHandler (through the IRouteHandler abstraction),
This method call creates a Route instance with the MVCRouteHandler set. You can always override it, as shown.
In current Routing (System.Web.Routing), routes are registered into a RouteCollection, which forms a linear collection of all the possible routes. When the routes are being processed against an incoming URL, they form a top-down queue, where the first Route that matches it wins. For ASP.NET 4.x, there can only be one route collection for your Application, so all of your routing requirements have to be fulfilled by this instance.
ASP.NET Core Routing
The new Routing framework is based around the concept of an IRouter,
An instance of RouterMiddleware can be created, using any instance of IRouter. You can think of this as the root of a routing tree. Routers can be implemented any way and can be plugged directly into the pipeline, using middleware. To handle the classical routing collection (or route table), we now have an IRouteCollection abstraction, itself extending IRouter. This means that any RouteCollection instance acts as a router and can be used directly with the middleware.
Using Routing Middleware
To implement route in the .NET Core Application, first add NuGet package Microsoft.AspNetCore.Routing.
Add routing to the Service container in Startup.cs.
Routes must be configured in the Configure method in the Startup class. The sample given below uses API.
If you are configuring a single route, call app.UseRouter, which is passed in an IRouter instance. You won't need to call RouteBuilder. The framework provides a set of extension methods to creating routes such as
Some of these methods such as MapGet require a RequestDelegate to be provided. The RequestDelegate will be used as the route handler when the route matches. Other methods in this family allow configuring a middleware pipeline, which will be used as the route handler. If the Map method doesn't accept a handler, such as MapRoute, then it will use the DefaultHandler. The Map[Verb] methods use the constraints to limit the route to the HTTP Verb in the method name.
Pro WPF: Windows Presentation Foundation in .NET 3.0