zoukankan      html  css  js  c++  java
  • .net core2.0 自定义中间件

    一、中间件(Middleware)

      中间件是被组装成一个应用程序管道来处理请求和响应的软件组件。

      

    二、编写SimpleMiddleware

    using Microsoft.AspNetCore.Http;
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Threading.Tasks;
    
    namespace GrabNovelApi.MiddleWare
    {
        public class SimpleMiddleWare
        {
            private readonly RequestDelegate _next;
    
            public SimpleMiddleWare(RequestDelegate next)
            {
                _next = next;
            }
    
            public async Task Invoke(HttpContext context)
            {
    
                Console.WriteLine("invoke");
                await _next.Invoke(context);
            }
        }
    }
    

    三、再新建一个:SimpleMiddleWareExtensions.cs

      用起来总有点奇怪,居然不是继承一个基类

    using Microsoft.AspNetCore.Builder;
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Threading.Tasks;
    
    namespace GrabNovelApi.MiddleWare
    {
        public static class SimpleMiddleWareExtensions
        {
            public static IApplicationBuilder SimpleMiddleWare(this IApplicationBuilder builder)
            {
                return builder.UseMiddleware<SimpleMiddleWare>();
            }
        }
    }
    

    四、使用中间件

      

  • 相关阅读:
    正则表达式常用收集
    IIS 部署nodejs
    借助svn进行半自动多台服务器上线部署
    快速开发window服务器程序
    sql server 存储过程解密
    EF 剥坑
    测试常规需要测试的东西
    html5本次存储几种方式
    log4net 写日志配置
    js 获取定位信息
  • 原文地址:https://www.cnblogs.com/WangJunZzz/p/8708360.html
Copyright © 2011-2022 走看看