zoukankan      html  css  js  c++  java
  • .net core 中间件实战

    1、新建一个ASP.NET Core Web Application项目,选择空模板。

    2、新建一个类RequestIPMiddleware.cs

    using Microsoft.AspNetCore.Http;
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Threading.Tasks;
    
    namespace MiddlewareDemo
    {
        public class RequestIPMiddleware
        {
            private readonly RequestDelegate _next;
    
            public RequestIPMiddleware(RequestDelegate next)
            {
                _next = next;
            }
    
            public async Task Invoke(HttpContext context)
            {
                await context.Response.WriteAsync("to do something 
    
    ");
                await context.Response.WriteAsync("IP:" + context.Connection.RemoteIpAddress.ToString() + "
    
    ");
                await _next.Invoke(context);
            }
        }
    }
    

      

    3、再新建一个扩展类 RequestIPExtensions.cs

    using Microsoft.AspNetCore.Builder;
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Threading.Tasks;
    
    namespace MiddlewareDemo
    {
        public static class RequestIPExtensions
        {
            public static IApplicationBuilder UseRequestIP(this IApplicationBuilder builder)
            {
                return builder.UseMiddleware<RequestIPMiddleware>();
            }
        }
    }
    

    4、使用中间件。在Startup.cs中添加app.UseRequestIP():

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Threading.Tasks;
    using Microsoft.AspNetCore.Builder;
    using Microsoft.AspNetCore.Hosting;
    using Microsoft.AspNetCore.Http;
    using Microsoft.Extensions.DependencyInjection;
    
    namespace MiddlewareDemo
    {
        public class Startup
        {
            // This method gets called by the runtime. Use this method to add services to the container.
            // For more information on how to configure your application, visit https://go.microsoft.com/fwlink/?LinkID=398940
            public void ConfigureServices(IServiceCollection services)
            {
               
            }
    
            // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
            public void Configure(IApplicationBuilder app, IHostingEnvironment env)
            {
                if (env.IsDevelopment())
                {
                    app.UseDeveloperExceptionPage();
                }
                app.UseRequestIP();
                app.Run(async (context) =>
                {
                    await context.Response.WriteAsync("Hello World!");
                });
            }
        }
    }
    

    5、运行效果

      

  • 相关阅读:
    Windows SDK编程(Delphi版) 之 应用基础,楔子
    一个小问题引发的论证思考
    Delphi 组件开发教程指南(7)继续模拟动画显示控件
    用PyInstaller将python转成可执行文件exe笔记
    使用 .Net Memory Profiler 诊断 .NET 应用内存泄漏(方法与实践)
    Microsof Office SharePoint 2007 工作流开发环境搭建
    How to monitor Web server performance by using counter logs in System Monitor in IIS
    LINQ之Order By
    window 性能监视器
    内存泄露检测工具
  • 原文地址:https://www.cnblogs.com/lizhenhong/p/9150907.html
Copyright © 2011-2022 走看看