这里的中间件概念和express.js中的是一致的,
下面栗子的代码来源: Net Core中间件封装原理示例demo解析1
2
3
4
5
6
7public static class RequestIpExtensions
{
public static IApplicationBuilder UseRequestIp(this IApplicationBuilder builder)
{
return builder.UseMiddleware<RequestIPMiddleware>();
}
}
each middleware extension method is exposed on IApplicationBuilder through the Microsoft.AspNetCore.Builder namespace. 通过IApplicationBuilder接口方法引入中间件,IApplicationBuilder由Microsoft.AspNetCore.Builder命名空间提供
中间件的具体实现1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17public 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);
}
}
使用中间
Startup.cs1
2
3
4
5public void Configure(IApplicationBuilder app, IHostingEnvironment env)
{
....
app.UseRequestIp();
}