ASP.NET Core -中间件(Middleware)使用
2018-05-15 13:53
1031 查看
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 Core 中间件Diagnostics使用
- ASP.NET Core 开发-中间件(Middleware)
- ASP.NET Core应用针对静态文件请求的处理[5]: DefaultFilesMiddleware中间件如何显示默认页面
- 学习ASP.NET Core, 怎能不了解请求处理管道[5]: 中间件注册可以除了可以使用Startup之外,还可以选择StartupFilter
- ASP.NET Core应用的错误处理[4]:StatusCodePagesMiddleware中间件如何针对响应码呈现错误页面
- 在ASP.NET Core使用Middleware模拟Custom Error Page功能
- ASP.NET Core 中间件(Middleware)详解
- 在ASP.NET Core Identity外面使用Cookie中间件
- asp.net core AuthenticationMiddleware 在WebApi中的的使用
- 使用Visual Studio Code开发Asp.Net Core WebApi学习笔记(四)-- Middleware
- ASP.NET Core 使用 URL Rewrite 中间件实现 HTTP 重定向到 HTTPS
- ASP.NET Core 运行原理剖析2:Startup 和 Middleware(中间件)
- ASP.NET Core 运行原理剖析2:Startup 和 Middleware(中间件)
- ASP.NET Core应用针对静态文件请求的处理[4]: DirectoryBrowserMiddleware中间件如何呈现目录结构
- ASP.NET Core 运行原理剖析2:Startup 和 Middleware(中间件)
- ASP.NET Core 中间件Diagnostics使用
- 详解在ASP.NET Core 中使用Cookie中间件
- ASP.NET Core 开发-中间件(StaticFiles)使用
- 在ASP.NET Core 中使用Cookie中间件
- ASP.NET Core中间件(Middleware)实现WCF SOAP服务端解析