【ASP.NET Core快速入門】(九) RoutingMiddleware介紹以及MVC引入
阿新 • • 發佈:2017-12-27
pre configure onf mvc tin 常用 esp red 引入
前言
前面我們介紹了使用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"); }); });
【ASP.NET Core快速入門】(九) RoutingMiddleware介紹以及MVC引入