Asp.net Core - Custom middleware

public class PrintSomethingMiddleware
    {
        private readonly RequestDelegate _next;
        public PrintSomethingMiddleware(RequestDelegate next)
        {
            _next = next;
        }

        public async Task InvokeAsync(HttpContext context)
        {
            // Print headers
            foreach (var header in context.Request.Headers)
            {
                Console.WriteLine(header.Key + "-" + header.Value);
            }

            // Call the next delegate/middleware in the pipeline
            await _next(context);
        }
    }
public static class IApplicationBuilderExtensions
    {
        public static IApplicationBuilder UsePrintSomething(this IApplicationBuilder builder)
        {
            return builder.UseMiddleware<PrintSomethingMiddleware>();
        }
    }
app.UsePrintSomething();
发布了130 篇原创文章 · 获赞 20 · 访问量 30万+

猜你喜欢

转载自blog.csdn.net/yuxuac/article/details/103548577