菜鸟入门【ASP.NET Core】9:RoutingMiddleware介绍以及MVC引入

前言

前面介绍了使用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");
                });
            });

posted on 2018-01-29 14:06  快舔包我很肥  阅读(207)  评论(0编辑  收藏  举报

导航