1.介绍
2.组成
3.中间件的合作伙伴
//自定义中间件,https://localhost:44391/test 请求结果为:
/*
1 start
2 start
Run
2 end
1 end
*/
//按照顺序执行每个Use的前逻辑,遇到Run之后再反向执行每个Use的后逻辑
//Run相当于一个终结点,Run之后的中间件不会被执行,因为它只有一个RequestDelegate类型参数不可以调用next.Invoke();
app.Map("/test", async pipeBuilder => {
pipeBuilder.Use(async (context, next) => {
context.Response.ContentType = "text/html";
await context.Response.WriteAsync("1 start<br/>");
await next.Invoke();
await context.Response.WriteAsync("1 end<br/>");
});
pipeBuilder.Use(async (context, next) => {
await context.Response.WriteAsync("2 start<br/>");
await next.Invoke();
await context.Response.WriteAsync("2 end<br/>");
});
pipeBuilder.Run(async context => {
await context.Response.WriteAsync("Run<br/>");
});
pipeBuilder.Use(async (context, next) => {
await context.Response.WriteAsync("3 我在Run后面还能被执行吗?<br/>");
await next.Invoke();
await context.Response.WriteAsync("3 end<br/>");
});
});
标签:Run,await,中间件,介绍,next,WriteAsync,context,Response
From: https://www.cnblogs.com/long-live/p/mw.html