排名
6
文章
6
粉丝
16
评论
8
{{item.articleTitle}}
{{item.blogName}} : {{item.content}}
ICP备案 :渝ICP备18016597号-1
网站信息:2018-2025TNBLOG.NET
技术交流:群号656732739
联系我们:contact@tnblog.net
公网安备:
50010702506256


欢迎加群交流技术

中间件与过滤器的区别
https://www.tnblog.net/aojiancc2/article/details/2537
创建中间件RequestIPMyMiddleware
public class RequestIPMyMiddleware { private readonly RequestDelegate _next; private readonly ILogger _logger; public RequestIPMyMiddleware(RequestDelegate next, ILoggerFactory logger) { _next = next; _logger = logger.CreateLogger<RequestIPMyMiddleware>(); } /// <summary> /// 执行的方法 /// </summary> /// <param name="context"></param> /// <returns></returns> public async Task Invoke(HttpContext context) { _logger.LogInformation("自定义中间件被执行"); //执行超作 _logger.LogInformation("My Ip:" + context.Connection.RemoteIpAddress.ToString()); //执行下一个中间件 await _next.Invoke(context); } }
添加中间件的引用
运行效果:
使用扩展方法中转添加中间件
这样使用使用起来就更加方便了
public static class RequestIPMiddlewareExtands { public static IApplicationBuilder UseMyIp(this IApplicationBuilder app) { //链式编程 return app.UseMiddleware<RequestIPMyMiddleware>(); } }
使用
欢迎加群讨论技术,1群:677373950(满了,可以加,但通过不了),2群:656732739。有需要软件开发,或者学习软件技术的朋友可以和我联系~(Q:815170684)
评价