Web API study notes (5) - middleware (Middleware) and HTTP request pipeline (Http Request Pipeline)

1. HTTP Request Pipeline (Http Request Pipeline)

insert image description here
insert image description here

2. Middleware

2.1 Overview

1. Middleware is a piece of code used in the HTTP request pipeline
2.Asp.Net Core application can have n middleware
3. The order of middleware is very important in the execution process.

Middleware example:
1.Routing
2.Authentication
3.Add excretion page …

2.2 Use of Run(), Use(), Next() and Map() in middleware

1.Run() method is used to complete the execution of middleware
2.Use() method is used to insert new middleware in the pipeline
3.Next() method is used to pass execution to the next middleware
4.Map() method is used to map middleware to a specific URL

2.2.1 Run() method

Add a terminal middleware delegate to the application to the request pipeline.

IApplicationBuilder.Run(RequestDelegate handler)
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
        {
    
    
            app.Run(async (context) =>
            {
    
    
                await context.Response.WriteAsync("hello Run");
            });

            app.Run(async (context) =>
            {
    
    
                await context.Response.WriteAsync("hello Run2");
            });


            if (env.IsDevelopment())
            {
    
    
                app.UseDeveloperExceptionPage();
            }


            app.UseRouting();

            app.UseEndpoints(endponits =>
            {
    
    
                endponits.MapControllers();
            });
        }

operation result:
insert image description here

2.2.1 Use()&Next()

 public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
        {
    
    
            //app.Run(async (context) =>
            //{
    
    
            //    await context.Response.WriteAsync("hello Run");
            //});

            app.Use(async (context,next) =>
            {
    
    
                await context.Response.WriteAsync("hello Use1 \n");
            });

            app.Run(async (context) =>
            {
    
    
                await context.Response.WriteAsync("hello Run2");
            });


            if (env.IsDevelopment())
            {
    
    
                app.UseDeveloperExceptionPage();
            }


            app.UseRouting();

            app.UseEndpoints(endponits =>
            {
    
    
                endponits.MapControllers();
            });
        }

operation result:
insert image description here

 public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
        {
    
    
            //app.Run(async (context) =>
            //{
    
    
            //    await context.Response.WriteAsync("hello Run");
            //});

            app.Use(async (context,next) =>
            {
    
    
                await context.Response.WriteAsync("hello Use1 \n");
                await next();
            });

            app.Run(async (context) =>
            {
    
    
                await context.Response.WriteAsync("hello Run2");
            });


            if (env.IsDevelopment())
            {
    
    
                app.UseDeveloperExceptionPage();
            }


            app.UseRouting();

            app.UseEndpoints(endponits =>
            {
    
    
                endponits.MapControllers();
            });
        }

operation result:
insert image description here

public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
        {
    
    
            //app.Run(async (context) =>
            //{
    
    
            //    await context.Response.WriteAsync("hello Run");
            //});

            app.Use(async (context,next) =>
            {
    
    
                await context.Response.WriteAsync("hello Use1 \n");
                await next();
                await context.Response.WriteAsync("hello Use2 \n");
            });

            app.Run(async (context) =>
            {
    
    
                await context.Response.WriteAsync("hello Run2 \n");
            });


            if (env.IsDevelopment())
            {
    
    
                app.UseDeveloperExceptionPage();
            }


            app.UseRouting();

            app.UseEndpoints(endponits =>
            {
    
    
                endponits.MapControllers();
            });
        }

operation result:
insert image description here

public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
        {
    
    
            //app.Run(async (context) =>
            //{
    
    
            //    await context.Response.WriteAsync("hello Run");
            //});

            app.Use(async (context,next) =>
            {
    
    
                await context.Response.WriteAsync("hello Use1-1 \n");
                await next();
                await context.Response.WriteAsync("hello Use1-2 \n");
            });

            app.Use(async (context, next) =>
            {
    
    
                await context.Response.WriteAsync("hello Use2-1 \n");
                await next();
                await context.Response.WriteAsync("hello Use2-2 \n");
            });

            app.Run(async (context) =>
            {
    
    
                await context.Response.WriteAsync("hello Run \n");
            });


            if (env.IsDevelopment())
            {
    
    
                app.UseDeveloperExceptionPage();
            }


            app.UseRouting();

            app.UseEndpoints(endponits =>
            {
    
    
                endponits.MapControllers();
            });
        }

operation result:
insert image description here

public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
        {
    
    
            //app.Run(async (context) =>
            //{
    
    
            //    await context.Response.WriteAsync("hello Run");
            //});

            app.Use(async (context,next) =>
            {
    
    
                await context.Response.WriteAsync("hello Use1-1 \n");
                await next();
                await context.Response.WriteAsync("hello Use1-2 \n");
            });

            app.Use(async (context, next) =>
            {
    
    
                await context.Response.WriteAsync("hello Use2-1 \n");
                await next();
                await context.Response.WriteAsync("hello Use2-2 \n");
            });

            app.Use(async (context,next) =>
            {
    
    
                await context.Response.WriteAsync("request complete \n");
            });

            //app.Run(async (context) =>
            //{
    
    
            //    await context.Response.WriteAsync("hello Run \n");
            //});


            if (env.IsDevelopment())
            {
    
    
                app.UseDeveloperExceptionPage();
            }


            app.UseRouting();

            app.UseEndpoints(endponits =>
            {
    
    
                endponits.MapControllers();
            });
        }

operation result:
insert image description here

2.2.3 Map()

Branches the request pipeline based on a match for a given request path. Branch is executed if the requested path starts with the given path.

        public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
        {
    
    
            //app.Run(async (context) =>
            //{
    
    
            //    await context.Response.WriteAsync("hello Run");
            //});

            app.Use(async (context,next) =>
            {
    
    
                await context.Response.WriteAsync("hello Use1-1 \n");
                await next();
                await context.Response.WriteAsync("hello Use1-2 \n");
            });

            app.Map("/nitish", Cutom);

            app.Use(async (context, next) =>
            {
    
    
                await context.Response.WriteAsync("hello Use2-1 \n");
                await next();
                await context.Response.WriteAsync("hello Use2-2 \n");
            });

            app.Use(async (context,next) =>
            {
    
    
                await context.Response.WriteAsync("request complete \n");
            });

            //app.Run(async (context) =>
            //{
    
    
            //    await context.Response.WriteAsync("hello Run \n");
            //});


            if (env.IsDevelopment())
            {
    
    
                app.UseDeveloperExceptionPage();
            }


            app.UseRouting();

            app.UseEndpoints(endponits =>
            {
    
    
                endponits.MapControllers();
            });
        }

        private void Cutom(IApplicationBuilder app)
        {
    
    
            app.Use(async (context, next) =>
            {
    
    
                await context.Response.WriteAsync("Hello nitish \n");
            });
        }

operation result:insert image description here

2.3 Custom middleware

1. Create a CustomMiddleware1.cs script, the code is as follows

using System.Threading.Tasks;
using Microsoft.AspNetCore.Http;

namespace MyWebApi
{
    
    
    public class CustomMiddleware1:IMiddleware
    {
    
    

        public async Task InvokeAsync(HttpContext context, RequestDelegate next)
        {
    
    
            await context.Response.WriteAsync("hello file-1 \n");
            await next(context);
            await context.Response.WriteAsync("hello file-2 \n");
        }
    }
}

2. Add a custom temporary service in the ConfigureServices method in the Startup.cs script, the code is as follows

        public void ConfigureServices(IServiceCollection services)
        {
    
    
            services.AddControllers();
            services.AddTransient<CustomMiddleware1>();
        }

2. Use custom middleware in the Configure method in the Startup.cs script, the code is as follows

public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
        {
    
    
            //app.Run(async (context) =>
            //{
    
    
            //    await context.Response.WriteAsync("hello Run");
            //});

            app.Use(async (context,next) =>
            {
    
    
                await context.Response.WriteAsync("hello Use1-1 \n");
                await next();
                await context.Response.WriteAsync("hello Use1-2 \n");
            });

            app.UseMiddleware<CustomMiddleware1>();

            app.Map("/nitish", Cutom);

            app.Use(async (context, next) =>
            {
    
    
                await context.Response.WriteAsync("hello Use2-1 \n");
                await next();
                await context.Response.WriteAsync("hello Use2-2 \n");
            });

            app.Use(async (context,next) =>
            {
    
    
                await context.Response.WriteAsync("request complete \n");
            });

            //app.Run(async (context) =>
            //{
    
    
            //    await context.Response.WriteAsync("hello Run \n");
            //});


            if (env.IsDevelopment())
            {
    
    
                app.UseDeveloperExceptionPage();
            }


            app.UseRouting();

            app.UseEndpoints(endponits =>
            {
    
    
                endponits.MapControllers();
            });
        }

operation result:
insert image description here

2.4 Verify the use of middleware

As mentioned before, Routing uses middleware. Next, we will use Routing as an example to verify
the source link of Asp.Net Core: https://github.com/dotnet/aspnetcore
and then search for UseRouting in the upper right corner, and find the method as shown below Shown:
insert image description here
Search for EndpointRoutingMiddleware.cs again, and the following method insert image description here
determines that the middleware next method is used in UseRouting.

Next section: Link: Web API Study Notes (6) - Routing (Routing)

Guess you like

Origin blog.csdn.net/weixin_45724919/article/details/126699796