asp.net core3.1 實戰開發(中介軟體的詳解)

2b勿擾發表於2020-01-04

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/>");
       }
   }
}

相關文章