前言
前面我们介绍了使用app.Map来配置路由,但是对于一般不是特别大的项目来说,我们不使用Map来进行路由配置。
配置路由
我们首先需要在Startup.cs文件中的ConfigureServices方法中进行路由依赖注入
services.AddRouting();
接下来我们就可以在Configure中使用扩展方法进行注册路由
//第一种方式app.UseRouter(builder=>builder.MapGet("actionfirst",async context =>{await context.Response.WriteAsync("this is first action");}));//第二种方式RequestDelegate handler=context=>context.Response.WriteAsync("this is second action");var route=new Route(new RouteHandler(handler),"actionsecond",app.ApplicationServices.GetRequiredService<IInlineConstraintResolver>());app.UseRouter(route);//第三种方式:不常用app.Map("/task",taskApp=>{taskApp.Run(async context=>{await context.Response.WriteAsync("this is a task");});});