添加readme.md文件
This commit is contained in:
parent
65b2336be5
commit
a0f3d367f0
55
README.MD
Normal file
55
README.MD
Normal file
|
@ -0,0 +1,55 @@
|
||||||
|
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
|
||||||
|
```
|
Loading…
Reference in New Issue
Block a user