Home >Backend Development >C#.Net Tutorial >What is routing in C# ASP.NET Core?

What is routing in C# ASP.NET Core?

WBOY
WBOYforward
2023-08-25 22:37:06856browse

C# ASP.NET Core 中的路由是什么?

Routes are used to map requests to route handlers.

Routes are configured when the application starts and can be accessed from The URL that will be used for request processing.

Routing basics

Routing uses routing (implementation of IRouter)

  • Map incoming request routing handler
  • Used in generating responses URL

Routes are connected to the middleware pipeline through the RouterMiddleware class. ASP.NET MVC adds routes to the middleware pipeline as part of its configuration

URL matching

Incoming requests go into the RouterMiddleware that calls the RouteAsync method

IRouter instance is set by RouteContext handler for a non-null RequestDelegate.

If the handler has a route set, it will be called to handle the request and will go no further Routing will be processed.

If all routes are executed and no handler for the request is found, the middleware will call next and the next middleware in the request pipeline are called.

URL Generation

URL generation follows a similar iterative process, but starts with the user or frame Code that calls the route collection's GetVirtualPath method.

Each route then calls its GetVirtualPath method in sequence until Returns non-null VirtualPathData

Create route

Routing provides the Route class as the standard implementation of IRouter. Routes use route template syntax to define what will be used with The URL path when calling RouteAsync.

When GetVirtualPath is , Route will use the same route template to generate the URL transfer.

Example

routes.MapRoute(name: "default", template: "{controller=Home}/{action=Index}/{id?}");

The framework provides a set of extension methods for creating routes, such as -

MapRoute
MapGet
MapPost
MapPut
MapRoute
MapVerb

The above is the detailed content of What is routing in C# ASP.NET Core?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:tutorialspoint.com. If there is any infringement, please contact admin@php.cn delete