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

5: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
"
); else { await context.Response.WriteAsync($"{nameof(MiddleWare)},Hello World MiddleWare!
"
); await _next(context); await context.Response.WriteAsync($"{nameof(MiddleWare)},Hello World MiddleWare!
"
); } } }