30-RoutingMiddleware介绍以及MVC引入

1-构建路由

 public class Startup
    {
        // This method gets called by the runtime. Use this method to add services to the container.
        // For more information on how to configure your application, visit https://go.microsoft.com/fwlink/?LinkID=398940
        public void ConfigureServices(IServiceCollection services)
        {
            services.AddRouting(); //如果采用第一种方式,必要加上些条命令
        }

        // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
        public void Configure(IApplicationBuilder app, IHostingEnvironment env)
        {
            if (env.IsDevelopment())
            {
                app.UseDeveloperExceptionPage();
            }

           //第一种方式
            app.UseRouter(myhandler=>{
                myhandler.MapGet("action",context=>context.Response.WriteAsync("this is action") );
            });

          //第一种方式,自己构建route
            RequestDelegate handler = context =>  context.Response.WriteAsync("i am router"); 
            var router = new Route(
                new RouteHandler(handler),
                "myaction",
                app.ApplicationServices.GetRequiredService<IInlineConstraintResolver>()     
            );
            app.UseRouter(router);

            //第二种方式
            app.Map("/task",taskApp=>{ //路由匹配
                taskApp.Run(async (context)=>{
                    await context.Response.WriteAsync("i am task");
                });
            });

        }
    }

 

posted @ 2018-07-21 12:45  深圳丶追  阅读(115)  评论(0编辑  收藏  举报