asp.net core3.1 实战开发(中间件的详解)

core3.1的中间件有点像前端express,koa,connect的用法比较统一好理解用法大概有如下几种方式

以下的代码都是在Startup的Configure函数中

一:中断式中间件,直接停止了流程。

app.Run(context => context.Response.WriteAsync("后续代码不会在执行了!"));

二:Use中间件第一种用法,返回RequestDelegate

app.Use(next =>
{
    Console.WriteLine("This is middleware 1");
    return new RequestDelegate(
        async context =>
        {
        //            context.Response.OnStarting(state =>
            //            {
            //                var httpContext = (HttpContext)state;
            //                httpContext.Response.Headers.Add("middleware", "12345");
            //                return Task.CompletedTask;
            //            }, context);
            await context.Response.WriteAsync("This is Hello World 1 start");
            await next.Invoke(context);
            await context.Response.WriteAsync("This is Hello World 1   end");
        });
});
app.Use(next =>
{
    Console.WriteLine("This is middleware 1.5");
    return new RequestDelegate(
        async context =>
        {
            await context.Response.WriteAsync("This is Hello World 1.5 start");
            await next.Invoke(context);
            await context.Response.WriteAsync("This is Hello World 1.5   end");
        });
});
app.Use(next =>
{
    Console.WriteLine("This is middleware 1.6");
    return new RequestDelegate(
        async context =>
        {
            await context.Response.WriteAsync("This is Hello World 1.6 start");
            await next.Invoke(context);
        });
});
app.Use(next =>
{
    Console.WriteLine("This is middleware 1.7");
    return new RequestDelegate(
        async context =>
        {
            await next.Invoke(context);
            await context.Response.WriteAsync("This is Hello World 1.7   end");
        });
});
app.Use(next =>
{
    Console.WriteLine("This is middleware 2");
    return new RequestDelegate(
        async context =>
        {
            await context.Response.WriteAsync("This is Hello World 2 start");
            await next.Invoke(context);
            await context.Response.WriteAsync("This is Hello World 2   end");
        });
});
app.Use(next =>
{
    Console.WriteLine("This is middleware 3");
    return new RequestDelegate(
        async context =>
        {
            await context.Response.WriteAsync("This is Hello World 3 start");
            //await next.Invoke(context);//最后这个没有执行Next
            await context.Response.WriteAsync("This is the one!");
            await context.Response.WriteAsync("This is Hello World 3   end");
        });
});

三:Use中间件第二种用法,无返回值

app.Use(async (context, next) =>//没有调用 next() 那就是终结点  跟Run一样
{
    await context.Response.WriteAsync("Hello World Use3  Again Again <br/>");
    //await next();
});

三:UseWhen可以对HttpContext检测后,增加处理环节;原来的流程还是正常执行的

app.UseWhen(context =>
{
    return context.Request.Query.ContainsKey("Name");
},
appBuilder =>
{
    appBuilder.Use(async (context, next) =>
    {
        await context.Response.WriteAsync("Hello World Use3 Again Again Again <br/>");
        //await next();
    });
});

四:Map根据条件指定中间件 指向终结点,没有Next

app.Map("/index", MapTest);//MapTest为函数
app.Map("/login", a => a.Run(async context =>
{
    await context.Response.WriteAsync($"This is Advanced Eleven Site");
}));
app.MapWhen(context =>
{
    return context.Request.Query.ContainsKey("Name");
    //拒绝非chorme浏览器的请求  
    //多语言
    //把ajax统一处理
}, MapTest);//MapTest为函数

五:UseMiddlerware 类–反射找

app.UseMiddleware<MiddleWare>();
public class MiddleWare
{
   private readonly RequestDelegate _next;

   public ThreeMiddleWare(RequestDelegate next)
   {
       _next = next;
   }

   public async Task Invoke(HttpContext context)
   {
       if (context.Request.Path.Value.Contains("XT"))
           await context.Response.WriteAsync($"{nameof(MiddleWare)}This is End<br/>");
       else
       {
           await context.Response.WriteAsync($"{nameof(MiddleWare)},Hello World MiddleWare!<br/>");
           await _next(context);
           await context.Response.WriteAsync($"{nameof(MiddleWare)},Hello World MiddleWare!<br/>");
       }
   }
}
发布了143 篇原创文章 · 获赞 117 · 访问量 4249

猜你喜欢

转载自blog.csdn.net/weixin_41181778/article/details/103838467