ASP.NET Core中间件库
Falcon.Middleware | ||
Falcon.Middleware.Test | ||
.gitignore | ||
Falcon.Middleware.sln | ||
README.MD |
ASP.NET CORE中间件相关扩展。
中间件基类MiddlewareBase
可以自行继承并实现自己的中间件类。例如:
/// <summary>
/// 测试中间件
/// </summary>
public class MiddlewareTest:MiddlewareBase
{
public MiddlewareTest(RequestDelegate next) : base(next) {
}
/// <summary>
/// 在调用next之后执行
/// </summary>
/// <param name="context">请求上下文</param>
public async override void InvokeNextAfter(HttpContext context) {
await context.Response.WriteAsync("\nInvokeNextAfter");
}
/// <summary>
/// 在调用next之前执行
/// </summary>
/// <param name="context">请求上下文</param>
public async override void InvokeNextBefore(HttpContext context) {
await context.Response.WriteAsync("InvokeNextBefore\n");
}
}
然后修改setup.cs
public void Configure(IApplicationBuilder app,IWebHostEnvironment env) {
if(env.IsDevelopment()) {
app.UseDeveloperExceptionPage();
}
app.UseRouting();
app.UseMiddleware<MiddlewareTest>();
app.UseEndpoints(endpoints => {
endpoints.MapGet("/",async context => {
await context.Response.WriteAsync("Hello World!");
});
});
}
浏览器发送请求后:
InvokeNextBefore
Hello World!
InvokeNextAfter