ASP.NET CORE 中间件

中间件的简单用法

代码中对于中间件的定义,通过翻看IApplicationBUilder的元数据,发现下面的两处定义对于形参的描述均为middleware,可见对于这两中委托均属于中间件。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
public interface IApplicationBuilder
{
//
// 摘要:
// Adds a middleware delegate to the application's request pipeline.
//
// 参数:
// middleware:
// The middleware delegate.
//
// 返回结果:
// The Microsoft.AspNetCore.Builder.IApplicationBuilder.
IApplicationBuilder Use(Func<RequestDelegate, RequestDelegate> middleware);
}

上面这种中间用来进行中间件间的转发,里面根据业务情况来决定处理请求发给哪个中间件。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
public static class UseExtensions
{
//
// 摘要:
// Adds a middleware delegate defined in-line to the application's request pipeline.
//
// 参数:
// app:
// The Microsoft.AspNetCore.Builder.IApplicationBuilder instance.
//
// middleware:
// A function that handles the request or calls the given next function.
//
// 返回结果:
// The Microsoft.AspNetCore.Builder.IApplicationBuilder instance.
public static IApplicationBuilder Use(this IApplicationBuilder app, Func<HttpContext, Func<Task>, Task> middleware);
}
  • 作为终端的中间件
1
2
3
4
5
6
7
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
app.Run(async context =>
{
await context.Response.WriteAsync("This is a middleware");
});
}
  • 加入两个中间件
1
2
3
4
5
6
7
8
9
10
11
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
app.Use(async (context, next) =>
{
await next.Invoke();
});
app.Run(async context =>
{
await context.Response.WriteAsync("This is a middleware");
});
}
  • 通过匹配路径使用分支
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
app.Map("/tom", app =>
{
app.Run(async context =>
{
await context.Response.WriteAsync("hello tom");
});
});

app.Map("/cat", app =>
{
app.Run(async context =>
{
await context.Response.WriteAsync("hello cat");
});
});

app.Map("", app =>
{
app.Run(async context =>
{
await context.Response.WriteAsync("hello everyone");
});
});
}
  • 通过自定义条件使用分支,判断规则按代码顺序判定
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
app.MapWhen(context =>
{
if (context.Request.Path.Value.Contains("dog"))
return true;
return false;
}, app =>
{
app.Run(async context =>
{
await context.Response.WriteAsync("hello dog");
});
}
);

app.Map("/tom", app =>
{
app.Run(async context =>
{
await context.Response.WriteAsync("hello tom");
});
});

app.Map("", app =>
{
app.Run(async context =>
{
await context.Response.WriteAsync("hello everyone");
});
});
}

  • 嵌套Map + 多段匹配
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
app.Map("/animal", builder =>
{
builder.Map("/dog", builder2 =>
{
builder2.Run(async context =>
{
await context.Response.WriteAsync("hello dog");
});
});
});

app.Map("/people/tom", builder =>
{
builder.Run(async context =>
{
await context.Response.WriteAsync("hello tom");
});
});
}
  • 中间件的短路
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
app.Use(GetRequestSchemeMiddleware);
app.Use(GetRequestMethodMiddleware);
app.Use(async (httpContext, func) =>
{
await func.Invoke();
});
app.Use((httpContext, func) =>
{
if (httpContext.Request.Path.Value.Contains("snapped"))
return httpContext.Response.WriteAsync("be snapped\r\n");
return func.Invoke();
});
app.Run(async context =>
{
await context.Response.WriteAsync("Terminal\r\n");
});
}

public static Task GetRequestSchemeMiddleware(HttpContext context,Func<Task> next)
{
string line = $"Request Method:{context.Request.Scheme}";
next.Invoke();
return context.Response.WriteAsync(line + "\r\n");
}
public static async Task GetRequestMethodMiddleware(HttpContext context, Func<Task> next)
{
string line = $"Request Method:{context.Request.Method}";
await next.Invoke();
await context.Response.WriteAsync(line + "\r\n");
}