1.HTTP请求管线(Http Request Pipeline)
2.中间件(Middleware)
2.1概述
1.中间件是HTTP请求管线中使用的一段代码
2.Asp .Net Core应用程序可以有n个中间件
3.中间件的顺序在执行过程中非常重要。
中间件例如:
1.Routing
2.Authentication
3.Add excretion page …
2.2Run(),Use(),Next()和Map()在中间件中的使用
1.Run()方法用于完成中间件的执行
2.Use()方法用于在管线中插入新的中间件
3.Next()方法用于将执行传递给下一个中间件
4.Map()方法用于将中间件映射到特定的URL
2.2.1 Run()方法
添加一个终端中间件委托到应用到请求管线。
IApplicationBuilder.Run(RequestDelegate handler)
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
app.Run(async (context) =>
{
await context.Response.WriteAsync("hello Run");
});
app.Run(async (context) =>
{
await context.Response.WriteAsync("hello Run2");
});
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}
app.UseRouting();
app.UseEndpoints(endponits =>
{
endponits.MapControllers();
});
}
运行结果:
2.2.1 Use()&Next()
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
//app.Run(async (context) =>
//{
// await context.Response.WriteAsync("hello Run");
//});
app.Use(async (context,next) =>
{
await context.Response.WriteAsync("hello Use1 \n");
});
app.Run(async (context) =>
{
await context.Response.WriteAsync("hello Run2");
});
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}
app.UseRouting();
app.UseEndpoints(endponits =>
{
endponits.MapControllers();
});
}
运行结果:
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
//app.Run(async (context) =>
//{
// await context.Response.WriteAsync("hello Run");
//});
app.Use(async (context,next) =>
{
await context.Response.WriteAsync("hello Use1 \n");
await next();
});
app.Run(async (context) =>
{
await context.Response.WriteAsync("hello Run2");
});
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}
app.UseRouting();
app.UseEndpoints(endponits =>
{
endponits.MapControllers();
});
}
运行结果:
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
//app.Run(async (context) =>
//{
// await context.Response.WriteAsync("hello Run");
//});
app.Use(async (context,next) =>
{
await context.Response.WriteAsync("hello Use1 \n");
await next();
await context.Response.WriteAsync("hello Use2 \n");
});
app.Run(async (context) =>
{
await context.Response.WriteAsync("hello Run2 \n");
});
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}
app.UseRouting();
app.UseEndpoints(endponits =>
{
endponits.MapControllers();
});
}
运行结果:
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
//app.Run(async (context) =>
//{
// await context.Response.WriteAsync("hello Run");
//});
app.Use(async (context,next) =>
{
await context.Response.WriteAsync("hello Use1-1 \n");
await next();
await context.Response.WriteAsync("hello Use1-2 \n");
});
app.Use(async (context, next) =>
{
await context.Response.WriteAsync("hello Use2-1 \n");
await next();
await context.Response.WriteAsync("hello Use2-2 \n");
});
app.Run(async (context) =>
{
await context.Response.WriteAsync("hello Run \n");
});
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}
app.UseRouting();
app.UseEndpoints(endponits =>
{
endponits.MapControllers();
});
}
运行结果:
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
//app.Run(async (context) =>
//{
// await context.Response.WriteAsync("hello Run");
//});
app.Use(async (context,next) =>
{
await context.Response.WriteAsync("hello Use1-1 \n");
await next();
await context.Response.WriteAsync("hello Use1-2 \n");
});
app.Use(async (context, next) =>
{
await context.Response.WriteAsync("hello Use2-1 \n");
await next();
await context.Response.WriteAsync("hello Use2-2 \n");
});
app.Use(async (context,next) =>
{
await context.Response.WriteAsync("request complete \n");
});
//app.Run(async (context) =>
//{
// await context.Response.WriteAsync("hello Run \n");
//});
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}
app.UseRouting();
app.UseEndpoints(endponits =>
{
endponits.MapControllers();
});
}
运行结果:
2.2.3 Map()
基于给定请求路径的匹配对请求管线进行分支。如果请求路径以给定的路径开始,则执行分支。
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
//app.Run(async (context) =>
//{
// await context.Response.WriteAsync("hello Run");
//});
app.Use(async (context,next) =>
{
await context.Response.WriteAsync("hello Use1-1 \n");
await next();
await context.Response.WriteAsync("hello Use1-2 \n");
});
app.Map("/nitish", Cutom);
app.Use(async (context, next) =>
{
await context.Response.WriteAsync("hello Use2-1 \n");
await next();
await context.Response.WriteAsync("hello Use2-2 \n");
});
app.Use(async (context,next) =>
{
await context.Response.WriteAsync("request complete \n");
});
//app.Run(async (context) =>
//{
// await context.Response.WriteAsync("hello Run \n");
//});
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}
app.UseRouting();
app.UseEndpoints(endponits =>
{
endponits.MapControllers();
});
}
private void Cutom(IApplicationBuilder app)
{
app.Use(async (context, next) =>
{
await context.Response.WriteAsync("Hello nitish \n");
});
}
运行结果:
2.3 自定义中间件
1.创建一个CustomMiddleware1.cs脚本,代码如下
using System.Threading.Tasks;
using Microsoft.AspNetCore.Http;
namespace MyWebApi
{
public class CustomMiddleware1:IMiddleware
{
public async Task InvokeAsync(HttpContext context, RequestDelegate next)
{
await context.Response.WriteAsync("hello file-1 \n");
await next(context);
await context.Response.WriteAsync("hello file-2 \n");
}
}
}
2.在Startup.cs脚本中ConfigureServices方法中添加一个自定义的临时服务,代码如下
public void ConfigureServices(IServiceCollection services)
{
services.AddControllers();
services.AddTransient<CustomMiddleware1>();
}
2.在Startup.cs脚本中Configure方法中使用自定义的中间件,代码如下
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
//app.Run(async (context) =>
//{
// await context.Response.WriteAsync("hello Run");
//});
app.Use(async (context,next) =>
{
await context.Response.WriteAsync("hello Use1-1 \n");
await next();
await context.Response.WriteAsync("hello Use1-2 \n");
});
app.UseMiddleware<CustomMiddleware1>();
app.Map("/nitish", Cutom);
app.Use(async (context, next) =>
{
await context.Response.WriteAsync("hello Use2-1 \n");
await next();
await context.Response.WriteAsync("hello Use2-2 \n");
});
app.Use(async (context,next) =>
{
await context.Response.WriteAsync("request complete \n");
});
//app.Run(async (context) =>
//{
// await context.Response.WriteAsync("hello Run \n");
//});
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}
app.UseRouting();
app.UseEndpoints(endponits =>
{
endponits.MapControllers();
});
}
运行结果:
2.4 验证中间件的使用
之前提到说Routing使用了中间件,接下来我们就以Routing为例来验证
Asp .Net Core源码链接: https://github.com/dotnet/aspnetcore
然后在右上角搜索UseRouting,找到该方法如下图所示:
再搜索EndpointRoutingMiddleware.cs,如下方法
确定UseRouting中使用了中间件next方法。
下一节:链接: Web API学习笔记(六)——路由(Routing)