中间件流程演示
中间件流程演示
Startup.cs
public void Configure(IApplicationBuilder app, IHostingEnvironment env,ILogger<Startup> logger)
{
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}
app.Use(async (context,next) =>
{
context.Response.ContentType = "text/plain;charset=utf-8";
logger.LogInformation("M1:传入请求");
//await context.Response.WriteAsync("第一个中间件");
await next();
logger.LogInformation("M1:传出响应");
});
app.Use(async (context, next) =>
{
context.Response.ContentType = "text/plain;charset=utf-8";
logger.LogInformation("M2:传入请求");
//await context.Response.WriteAsync("第二个中间件");
await next();
logger.LogInformation("M2:传出响应");
});
app.Run(async (context) =>
{
// 进程名
// var processName = System.Diagnostics.Process.GetCurrentProcess().ProcessName;
//var configVal = _configuration["MyKey"];
await context.Response.WriteAsync("M3:处理请求,并生成相应");
logger.LogInformation("M3:处理请求,并生成相应");
});
}
输出结果
StudentManagement.Startup:Information: M1:传入请求
StudentManagement.Startup:Information: M2:传入请求
StudentManagement.Startup:Information: M3:处理请求,并生成相应
StudentManagement.Startup:Information: M2:传出响应
StudentManagement.Startup:Information: M1:传出响应