asp.net core3.1 實戰開發(中介軟體的詳解)
core3.1的中介軟體有點像前端express,koa,connect的用法比較統一好理解用法大概有如下幾種方式
以下的程式碼都是在Startup的Configure函式中
一:中斷式中介軟體,直接停止了流程。
app.Run(context => context.Response.WriteAsync("後續程式碼不會在執行了!"));
二:Use中介軟體第一種用法,返回RequestDelegate
app.Use(next =>
{
Console.WriteLine("This is middleware 1");
return new RequestDelegate(
async context =>
{
// context.Response.OnStarting(state =>
// {
// var httpContext = (HttpContext)state;
// httpContext.Response.Headers.Add("middleware", "12345");
// return Task.CompletedTask;
// }, context);
await context.Response.WriteAsync("This is Hello World 1 start");
await next.Invoke(context);
await context.Response.WriteAsync("This is Hello World 1 end");
});
});
app.Use(next =>
{
Console.WriteLine("This is middleware 1.5");
return new RequestDelegate(
async context =>
{
await context.Response.WriteAsync("This is Hello World 1.5 start");
await next.Invoke(context);
await context.Response.WriteAsync("This is Hello World 1.5 end");
});
});
app.Use(next =>
{
Console.WriteLine("This is middleware 1.6");
return new RequestDelegate(
async context =>
{
await context.Response.WriteAsync("This is Hello World 1.6 start");
await next.Invoke(context);
});
});
app.Use(next =>
{
Console.WriteLine("This is middleware 1.7");
return new RequestDelegate(
async context =>
{
await next.Invoke(context);
await context.Response.WriteAsync("This is Hello World 1.7 end");
});
});
app.Use(next =>
{
Console.WriteLine("This is middleware 2");
return new RequestDelegate(
async context =>
{
await context.Response.WriteAsync("This is Hello World 2 start");
await next.Invoke(context);
await context.Response.WriteAsync("This is Hello World 2 end");
});
});
app.Use(next =>
{
Console.WriteLine("This is middleware 3");
return new RequestDelegate(
async context =>
{
await context.Response.WriteAsync("This is Hello World 3 start");
//await next.Invoke(context);//最後這個沒有執行Next
await context.Response.WriteAsync("This is the one!");
await context.Response.WriteAsync("This is Hello World 3 end");
});
});
三:Use中介軟體第二種用法,無返回值
app.Use(async (context, next) =>//沒有呼叫 next() 那就是終結點 跟Run一樣
{
await context.Response.WriteAsync("Hello World Use3 Again Again <br/>");
//await next();
});
三:UseWhen可以對HttpContext檢測後,增加處理環節;原來的流程還是正常執行的
app.UseWhen(context =>
{
return context.Request.Query.ContainsKey("Name");
},
appBuilder =>
{
appBuilder.Use(async (context, next) =>
{
await context.Response.WriteAsync("Hello World Use3 Again Again Again <br/>");
//await next();
});
});
四:Map根據條件指定中介軟體 指向終結點,沒有Next
app.Map("/index", MapTest);//MapTest為函式
app.Map("/login", a => a.Run(async context =>
{
await context.Response.WriteAsync($"This is Advanced Eleven Site");
}));
app.MapWhen(context =>
{
return context.Request.Query.ContainsKey("Name");
//拒絕非chorme瀏覽器的請求
//多語言
//把ajax統一處理
}, MapTest);//MapTest為函式
五:UseMiddlerware 類–反射找
app.UseMiddleware<MiddleWare>();
public class MiddleWare
{
private readonly RequestDelegate _next;
public ThreeMiddleWare(RequestDelegate next)
{
_next = next;
}
public async Task Invoke(HttpContext context)
{
if (context.Request.Path.Value.Contains("XT"))
await context.Response.WriteAsync($"{nameof(MiddleWare)}This is End<br/>");
else
{
await context.Response.WriteAsync($"{nameof(MiddleWare)},Hello World MiddleWare!<br/>");
await _next(context);
await context.Response.WriteAsync($"{nameof(MiddleWare)},Hello World MiddleWare!<br/>");
}
}
}
相關文章
- ASP.NET Core 中介軟體詳解及專案實戰ASP.NET
- asp.net core3.1 實戰開發(授權,鑑權封裝詳解)ASP.NET封裝
- asp.net core3.1 實戰開發(驗證碼的封裝和使用)ASP.NET封裝
- Express 實戰(四):中介軟體Express
- Express中介軟體原理詳解Express
- middleware 中介軟體詳解
- 資料庫中介軟體詳解資料庫
- 孔子=?中介軟體開發框架?框架
- Flutter完整開發實戰詳解(二、快速開發實戰篇)Flutter
- Flutter完整開發實戰詳解(十六、詳解自定義佈局實戰)Flutter
- (精華)2020年9月17日 ASP.NET Core 中介軟體詳解ASP.NET
- ASP.NET Core 中介軟體基本用法ASP.NET
- ASP.NET Core - 自定義中介軟體ASP.NET
- [譯]ASP.NET Core 2.0 中介軟體ASP.NET
- SpringBoot分散式任務中介軟體開發 附視訊講解 (手把手教你開發和使用中介軟體)Spring Boot分散式
- Redux中介軟體之redux-thunk使用詳解Redux
- 中介軟體:ElasticSearch元件RestHighLevelClient用法詳解Elasticsearch元件RESTclient
- 基於gin的golang web開發:中介軟體GolangWeb
- ASP.NET Core 中介軟體(Middleware)(一)ASP.NET
- asp.net core mvc 管道之中介軟體ASP.NETMVC
- ASP.NET Core中的中介軟體及其工作原理ASP.NET
- Koa日誌中介軟體封裝開發封裝
- 如何開發一個框架或中介軟體框架
- Flutter完整開發實戰詳解(二、 快速開發實戰篇) | 掘金技術徵文Flutter
- 解讀 Redux 中介軟體的原理Redux
- Redux 中介軟體的實現原理Redux
- Flutter開發實戰初級(一)ListView詳解FlutterView
- Flutter完整開發實戰詳解(五、 深入探索)Flutter
- 實戰!Spring Boot 整合 阿里開源中介軟體 Canal 實現資料增量同步!Spring Boot阿里
- ASP.NET Core中介軟體初始化探究ASP.NET
- ASP.NET Core - 請求管道與中介軟體ASP.NET
- 工具和中介軟體——redis,從底層原理到開發實踐Redis
- 精通中介軟體測試:Asp.Net Core實戰指南,提升應用穩定性和可靠性ASP.NET
- RocketMQ訊息中介軟體詳解(萬字圖文總結)MQ
- Redis中介軟體與Web中介軟體RedisWeb
- 從-1開始實現一箇中介軟體
- [分散式][訊息中介軟體]訊息中介軟體如何實現每秒幾十萬的高併發寫入分散式
- Flutter開發實戰初級(一)ListView詳解2FlutterView