zoukankan      html  css  js  c++  java
  • aspnetcore的中间件

    Run会终止中间件继续传递

     1 app.Run(new RequestDelegate(async context =>
     2 {
     3         await Task.Run(() =>
     4         {
     5                 context.Response.WriteAsync("被Run截取到了");
     6          });
     7 }));
     8 
     9 app.Run(new RequestDelegate(async context =>
    10 {
    11         await context.Response.WriteAsync("被Run截取到了");
    12 }));

     Use 会继续传递

    app.Use(async (context, next) =>
    {
            context.Response.Headers.Add("ResponseAt", DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss ffff"));
            await next();
    });

    Map和MapWhen

    app.Map("/Test", configuration =>
                {
                    configuration.Run(async context =>
                    {
                        await context.Response.WriteAsync("Test被截取了");
                    });
                });
    
                app.MapWhen(
                    context => context.Request.Path.Value.Contains("Test", StringComparison.CurrentCultureIgnoreCase),
                    configuration => configuration.Run(async context =>
                    {
                        await context.Response.WriteAsync("带Test的被截取了");
                    }));

    使用UseMiddleware

    public class MyMiddleware
        {
            private readonly RequestDelegate _next;
    
            private readonly ILogger _logger;
    
            private readonly string _prefix;
    
            public MyMiddleware(RequestDelegate next, ILoggerFactory factory, string prefix)
            {
                _next = next;
                _logger = factory.CreateLogger<MyMiddleware>();
                _prefix = prefix;
            }
    
    
            public async Task Invoke(HttpContext context)
            {
                _logger.LogInformation("" + _prefix + "" + context.Request.Path.Value);
                context.Response.Headers.Add("ResponseAt", DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss ffff"));
                await _next(context);
            }
        }
    app.UseMiddleware<MyMiddleware>("博客");
  • 相关阅读:
    网易2019校招C++研发工程师笔试编程题
    牛客网 数串
    ps aux 状态介绍
    阿里在线测评解析
    Ubuntu 18.04安装 Sublime
    file '/grub/i386-pc/normal.mod' not found.解决方案
    解决Windows10与Ubuntu系统时间不一致问题
    进程与线程的区别
    大端模式和小端模式
    2016湖南省赛----G
  • 原文地址:https://www.cnblogs.com/diwu0510/p/10810630.html
Copyright © 2011-2022 走看看