.net core 中间件使用
发布时间:2020-12-16 09:26:14 所属栏目:asp.Net 来源:网络整理
导读:原文: .net core 中间件使用 using System; using System.Collections.Generic; using System.Linq; using System.Threading.Tasks; using Microsoft.AspNetCore.Builder; using Microsoft.AspNetCore.Hosting; using Microsoft.AspNetCore.Http; using Micr
原文:
.net core 中间件使用
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.DependencyInjection;
namespace 中间件的原理和使用
{
public class Startup
{
// This method gets called by the runtime. Use this method to add services to the container.
// For more information on how to configure your application,visit https://go.microsoft.com/fwlink/?LinkID=398940
public void ConfigureServices(IServiceCollection services) {
}
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
//IApplicationBuilder app 管道接口
public void Configure(IApplicationBuilder app,IHostingEnvironment env) {
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}
//在使用中间件的时候要主要上下的位置,因为管道有先执行的好处和坏处
#region 测试实例1
////可以是管道短路,所以下面我要手动进入下一个管道
//app.Use(async (context,next) =>
//{
// await context.Response.WriteAsync("进入第一个委托管道 开始<br/>");
// //进入下一个管道
// await next.Invoke();
// await context.Response.WriteAsync("进入第一个委托管道 结束 进入下一委托管道<br/>");
//});
////app.Run是管道终结者
//app.Run(async (context) =>
//{
// await context.Response.WriteAsync("进入第二个委托管道 开始<br/>");
// await context.Response.WriteAsync("Hello World!<br/>");
// await context.Response.WriteAsync("进入第二个委托管道 结束<br/>");
//});
#endregion
//Map是一个分支管道约束,第一个参数是请求的Url,第二个是函数
app.Map("/tets1/Index",Test1);
app.Map("/tets2",Test2);
//也可以嵌套
app.Map("/level1",level1App => {
level1App.Map("/level2a",level2AApp => {
// "/level1/level2a"
//...
});
level1App.Map("/level2b",level2BApp => {
// "/level1/level2b"
//...
});
});
//MapWhen可以看出来他是根据条件分开管道的第一个参数是一个Func<HttpContext,bool> 如果是真的就进入第二个方法
app.MapWhen(context =>context.Request.Query.ContainsKey("Name"),Test3);
//注入中间件 这个是第一个,还有一个是比较常用的静态封装
app.UseMiddleware<Centre>();
//这个就是静态封装的可以很好的保护你的代码
app.UseFirstMiddleware();
app.Run(async (context) =>
{
await context.Response.WriteAsync("管道终结者来了");
});
}
/// <summary>
/// 第一个管道方法
/// </summary>
/// <param name="app"></param>
static void Test1(IApplicationBuilder app) {
app.Run(async (context) => {
await context.Response.WriteAsync("进入第一个测试委托管道");
});
}
/// <summary>
/// 第二个管道方法
/// </summary>
/// <param name="app"></param>
static void Test2(IApplicationBuilder app) {
app.Run(async (context) => {
await context.Response.WriteAsync("进入第一个测试委托管道");
});
}
/// <summary>
/// 第三个管道方法 但是他要满足一定的条件
/// </summary>
/// <param name="app"></param>
static void Test3(IApplicationBuilder app) {
app.Run(async (context) => {
await context.Response.WriteAsync("沙雕你进入了我的规则");
});
}
}
}
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Http;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
namespace 中间件的原理和使用
{
/// <summary>
/// 静态封装
/// </summary>
public static class CustomMiddlewareExtensions
{
public static IApplicationBuilder UseFirstMiddleware(this IApplicationBuilder builder) {
return builder.UseMiddleware<Centre>();
}
}
public class Centre
{
/// <summary>
/// 打管道注入进来
/// </summary>
private RequestDelegate _next;
public Centre(RequestDelegate next) {
_next = next;
}
public async Task Invoke(HttpContext context) {
await context.Response.WriteAsync($"进入我的中间件的类");
await _next(context);
}
}
}
上班没有什么时间,就直接上代码了,代码注释都有。 (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |