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、运行效果

      

  • 相关阅读:
    C#各自定义控件的网址链接
    给应用程序加装“看门狗”
    CRT detected that the application wrote to memory after after the end of heap buffer(这个经常忘掉)
    关于C#中的Delegate的一些知识
    实行项目管理信息化的好处
    C#连接池的详细分析(转)
    .Net Remoting和Web Service大比拼(转)
    使用SqlDataSource调用带参数存储过程插入数据
    156转换为byte时为什么会变成100的解释
    站在“组织者”这个角色上
  • 原文地址:https://www.cnblogs.com/lizhenhong/p/9150907.html
Copyright © 2011-2022 走看看