ASP.NET Core开发,开发并使用中间件(Middleware)。

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

每个组件选择是否传递给管道中的下一个组件的请求,并能之前和下一组分在管道中调用之后执行特定操作。

具体如图:

开发中间件(Middleware)

今天我们来实现一个记录ip 的中间件。

1.新建一个asp.net core项目,选择空的模板。

2.新建一个类: RequestIPMiddleware.cs

    public class RequestIPMiddleware
{
private readonly RequestDelegate _next;
private readonly ILogger _logger; public RequestIPMiddleware(RequestDelegate next, ILoggerFactory loggerFactory)
{
_next = next;
_logger = loggerFactory.CreateLogger<RequestIPMiddleware>();
} public async Task Invoke(HttpContext context)
{
_logger.LogInformation("User IP: " + context.Connection.RemoteIpAddress.ToString());
await _next.Invoke(context);
}
}

3.再新建一个:RequestIPExtensions.cs

    public static class RequestIPExtensions
{
public static IApplicationBuilder UseRequestIP(this IApplicationBuilder builder)
{
return builder.UseMiddleware<RequestIPMiddleware>();
}
}

这样我们就编写好了一个中间件。

使用中间件(Middleware)

1.使用

在 Startup.cs 添加 app.UseRequestIP()

        public void Configure(IApplicationBuilder app, ILoggerFactory loggerfactory)
{
loggerfactory.AddConsole(minLevel: LogLevel.Information);
app.UseRequestIP();//使用中间件
app.Run(async (context) =>
{
await context.Response.WriteAsync("Hello World!");
});
}

然后运行程序,我选择使用Kestrel 。

访问:http://localhost:5000/

成功运行。

二、Asp.Net Core使用中间件拦截处理请求

public class OuterImgMiddleware
{
public static string RootPath { get; set; } //配置文件读取绝对位置
private readonly RequestDelegate _next;
public OuterImgMiddleware(RequestDelegate next, IHostingEnvironment env)
{
// _wwwrootFolder = env.WebRootPath;
_next = next;
}
public async Task Invoke(HttpContext context)
{
var path = context.Request.Path.ToString();
var headersDictionary = context.Request.Headers; if (context.Request.Method == "GET")
if (!string.IsNullOrEmpty(path) && path.Contains("/upload/logo"))
{ //var unauthorizedImagePath = Path.Combine(RootPath, path);
var unauthorizedImagePath = RootPath + path;
await context.Response.SendFileAsync(unauthorizedImagePath);
return;
} await _next(context);
}
}
public static class MvcExtensions
{
public static IApplicationBuilder UseOutImg(this IApplicationBuilder builder)
{
return builder.UseMiddleware<OuterImgMiddleware>();
}
}

同上在Configure()中注册使用就可以了。

更多:

Asp.Net Core 通过自定义中间件防止图片盗链的实例(转)

在ASP.NET Core2.0中使用百度在线编辑器UEditor(转)

Asp.Net Core WebAPI入门整理(四)参数获取

最新文章

  1. EF 在controller 带参数跳转到新的网址
  2. Ubuntu Server 安装桌面untiy
  3. python海龟图制作
  4. sql中的字符串匹配、函数大全
  5. SQL 查询优化
  6. Oracle登陆及修改用户密码
  7. 使用ARM和VMSS创建自动扩展的web集群
  8. phpStudy速度慢的解决办法
  9. 手机自动化测试:Appium源码之API(2)
  10. [js高手之路] es6系列教程 - 新的类语法实战选项卡
  11. 第二篇--PCI设备解析
  12. iOS------App之间传递数据的几种方式
  13. View体系之属性动画
  14. Kosaraju与Tarjan(图的强连通分量)
  15. MVC 多submit
  16. JavaSE学习总结(十六)—— 泛型与泛型应用
  17. BZOJ.1299.[LLH邀请赛]巧克力棒(博弈论 Nim)
  18. thinkphp安装不成功可能跟数据库名有关
  19. BCompare破解方法
  20. php排序学习之-冒泡排序

热门文章

  1. 自适应电脑、手机和iPad的网页设计方法
  2. python 全栈开发,Day53(jQuery的介绍,jQuery的选择器,jQuery动画效果)
  3. 《剑指offer》-整数中1出现的次数
  4. 使用vmware提示无法打开内核设备 \\.\Global\vmx86: 系统找不到指定的文件
  5. 转:CentOS下后台运行Python脚本及关闭脚本的一些操作
  6. Codeforces 744C Hongcow Buys a Deck of Cards 状压dp (看题解)
  7. Jenkins Pipline语法
  8. Minimum Transport Cost HDU1385(路径打印)
  9. VSCode从非根目录编译golang程序(转)
  10. POJ 3273-Monthly Expense 求分组和的最小的最大值【二分答案】