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

      

  • 相关阅读:
    SQL中ISNULL的用法
    将日期类型转换成年月日的形式
    使用jQuery获取GridView的数据行的数量
    GridView数据源绑定的一个小问题
    less中混合
    封装(模块化)
    css实现一个缺口小三角
    淘宝的css初始化代码
    div+css制作表格
    浮动元素水平居中
  • 原文地址:https://www.cnblogs.com/lizhenhong/p/9150907.html
Copyright © 2011-2022 走看看