.net core 中间件使用

using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.DependencyInjection;

namespace 中间件的原理和使用
{
    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)
        {
        }

        // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.

        //IApplicationBuilder app 管道接口
        public void Configure(IApplicationBuilder app, IHostingEnvironment env)
        {
            if (env.IsDevelopment())
            {
                app.UseDeveloperExceptionPage();
            }
            //在使用中间件的时候要主要上下的位置,因为管道有先执行的好处和坏处

            #region 测试实例1
            ////可以是管道短路,所以下面我要手动进入下一个管道
            //app.Use(async (context, next) =>
            //{
            //    await context.Response.WriteAsync("进入第一个委托管道  开始
"); // //进入下一个管道 // await next.Invoke(); // await context.Response.WriteAsync("进入第一个委托管道 结束 进入下一委托管道
"); //}); ////app.Run是管道终结者 //app.Run(async (context) => //{ // await context.Response.WriteAsync("进入第二个委托管道 开始
"); // await context.Response.WriteAsync("Hello World!
"); // await context.Response.WriteAsync("进入第二个委托管道 结束
"); //}); #endregion //Map是一个分支管道约束,第一个参数是请求的Url,第二个是函数 app.Map("/tets1/Index", Test1); app.Map("/tets2", Test2); //也可以嵌套 app.Map("/level1", level1App => { level1App.Map("/level2a", level2AApp => { // "/level1/level2a" //... }); level1App.Map("/level2b", level2BApp => { // "/level1/level2b" //... }); }); //MapWhen可以看出来他是根据条件分开管道的第一个参数是一个Func 如果是真的就进入第二个方法 app.MapWhen(context =>context.Request.Query.ContainsKey("Name"), Test3); //注入中间件 这个是第一个,还有一个是比较常用的静态封装 app.UseMiddleware(); //这个就是静态封装的可以很好的保护你的代码 app.UseFirstMiddleware(); app.Run(async (context) => { await context.Response.WriteAsync("管道终结者来了"); }); } /// /// 第一个管道方法 /// /// static void Test1(IApplicationBuilder app) { app.Run(async (context) => { await context.Response.WriteAsync("进入第一个测试委托管道"); }); } /// /// 第二个管道方法 /// /// static void Test2(IApplicationBuilder app) { app.Run(async (context) => { await context.Response.WriteAsync("进入第一个测试委托管道"); }); } /// /// 第三个管道方法 但是他要满足一定的条件 /// /// static void Test3(IApplicationBuilder app) { app.Run(async (context) => { await context.Response.WriteAsync("沙雕你进入了我的规则"); }); } } }
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Http;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

namespace 中间件的原理和使用
{
    /// 
    /// 静态封装
    /// 
    public static class CustomMiddlewareExtensions
    {
        public static IApplicationBuilder UseFirstMiddleware(this IApplicationBuilder builder)
        {
            return builder.UseMiddleware();
        }
    }

    public class Centre
    {
        /// 
        /// 打管道注入进来
        /// 
        private RequestDelegate _next;
        public Centre(RequestDelegate next) {
            _next = next;
        }


        public async Task Invoke(HttpContext context) {
            await context.Response.WriteAsync($"进入我的中间件的类");
            await _next(context);
        }
    }

   
}

上班没有什么时间,就直接上代码了,代码注释都有。

你可能感兴趣的:(C#)