日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > asp.net >内容正文

asp.net

[ASP.NET Core] Middleware

發(fā)布時間:2023/12/4 asp.net 29 豆豆
生活随笔 收集整理的這篇文章主要介紹了 [ASP.NET Core] Middleware 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

前言

本篇文章介紹ASP.NET Core里,用來處理HTTP封包的Middleware,為自己留個紀錄也希望能幫助到有需要的開發(fā)人員。

  • ASP.NET Core官網(wǎng)

結(jié)構(gòu)

  • 在ASP.NET Core里,每個從「瀏覽器傳入」的HTTP Request封包,會被系統(tǒng)封裝為「HttpRequest對象」,并且配置默認的HttpResponse對象、Session對象、ClaimsPrincipal對象...等等物件。接著將這些對象,封裝成為一個「HttpContext對象」,用來提供ASP.NET Core后續(xù)使用。

    frameborder="0" scrolling="no" style="border-width: initial; border-style: none; width: 715px; height: 521px;">

  • ASP.NET Core在收到HttpContext之后,會把它交給一個「Pipeline」去處理。這個Pipeline里面配置很多「Middleware」。系統(tǒng)會將HttpContext,依序傳遞給Pipeline里的Middleware去處理。每個Middleware會依照自己內(nèi)部的程序邏輯,來運算處理HttpContext,并且變更HttpContext所封裝的對象內(nèi)容。

    frameborder="0" scrolling="no" style="border-width: initial; border-style: none; width: 715px; height: 521px;">

  • ASP.NET Core在收到經(jīng)由Middleware處理完畢的HttpContext之后,就會取出其中所封裝的HttpResponse對象。然后依照這個HttpResponse對象,來建立從「服務器回傳」的HTTP Response封包內(nèi)容。

    frameborder="0" scrolling="no" style="border-width: initial; border-style: none; width: 715px; height: 519px;">

  • ASP.NET Core經(jīng)由上述的系統(tǒng)結(jié)構(gòu),完成HTTP Request封包輸入、HTTP Response封包輸出的工作流程。

    frameborder="0" scrolling="no" style="border-width: initial; border-style: none; width: 715px; height: 519px;">

開發(fā)

Invoke

在[ASP.NET Core] Getting Started這篇文章里,提供了一個ASP.NET Core的Middleware范例:HelloWorldMiddleware。在這個范例里,Middleware透過實做Invoke方法,來提供自己所封裝的程序邏輯。

public class HelloWorldMiddleware {// Fieldsprivate readonly RequestDelegate _next;// Constructorspublic HelloWorldMiddleware(RequestDelegate next){_next = next;}// Methodspublic Task Invoke(HttpContext context){// Responsecontext.Response.WriteAsync("Hello World!");// Returnreturn Task.CompletedTask;} }

frameborder="0" scrolling="no" style="border-width: initial; border-style: none; width: 715px; height: 510px;">

HttpContext.Request

在實做Middleware.Invoke方法的時候,開發(fā)人員可以透過HttpContext.Request,來取得從「瀏覽器傳入」的HTTP Request封包內(nèi)容。在下列的范例程序代碼里,就是透過HttpContext.Request的Path、QueryString兩個屬性,來分別取得HTTP Request封包的URL路徑與QueryString內(nèi)容。

public class HelloWorldMiddleware {// Fieldsprivate readonly RequestDelegate _next;// Constructorspublic HelloWorldMiddleware(RequestDelegate next){_next = next;}// Methodspublic Task Invoke(HttpContext context){// Requeststring path = context.Request.Path.ToString();string queryString = context.Request.QueryString.ToString();string message = string.Format("path={0}, queryString={1}", path, queryString);// Responsecontext.Response.WriteAsync(message);// Returnreturn Task.CompletedTask;} }

frameborder="0" scrolling="no" style="border-width: initial; border-style: none; width: 715px; height: 510px;">

HttpContext.Response

同樣在實做Middleware.Invoke方法的時候,開發(fā)人員可以透過HttpContext.Response,來設定從「服務器回傳」的HTTP Response封包內(nèi)容。在下列的范例程序代碼里,就是透過HttpContext.Response的WriteAsync方法、StatusCode屬性,來分別設定HTTP Response封包的Content與StatusCode。

public class HelloWorldMiddleware {// Fieldsprivate readonly RequestDelegate _next;// Constructorspublic HelloWorldMiddleware(RequestDelegate next){_next = next;}// Methodspublic Task Invoke(HttpContext context){// Responsecontext.Response.StatusCode = 404;context.Response.WriteAsync("Not Found");// Returnreturn Task.CompletedTask;} }

frameborder="0" scrolling="no" style="border-width: initial; border-style: none; width: 715px; height: 510px;">

Exception

而在實做Middleware.Invoke方法的時候,如果程序代碼里發(fā)生了預期之外的Exception。ASP.NET Core預設會使用「500 Internal Server Error」,這個StatusCode來通報系統(tǒng)內(nèi)部發(fā)生異常。 在下列的范例程序代碼里,就是直接拋出一個例外錯誤,交由ASP.NET Core的錯誤處理機制去處理。

public class HelloWorldMiddleware {// Fieldsprivate readonly RequestDelegate _next;// Constructorspublic HelloWorldMiddleware(RequestDelegate next){_next = next;}// Methodspublic Task Invoke(HttpContext context){// Exceptionthrow new Exception();// Returnreturn Task.CompletedTask;} }

frameborder="0" scrolling="no" style="border-width: initial; border-style: none; width: 715px; height: 510px;">

RequestDelegate

建立Middleware的時候,開發(fā)人員可以透過建構(gòu)子所傳入的RequestDelegate,來參考到Pipeline里的下一個Middleware。透過調(diào)用RequestDelegate,就可以調(diào)用Pipeline里的下一個Middleware的Invoke方法。在下列的范例程序代碼里,就是透過調(diào)用RequestDelegate,來調(diào)用Pipeline里的下一個Middleware的Invoke方法,藉此串接其他Middleware的程序邏輯。

public class HelloWorldMiddleware {// Fieldsprivate readonly RequestDelegate _next;// Constructorspublic HelloWorldMiddleware(RequestDelegate next){_next = next;}// Methodspublic async Task Invoke(HttpContext context){// Do Something 01//....// Nextawait _next.Invoke(context);// Do Something 02// ...} }

參考

  • Middleware - ASP.NET Core

  • ASP.NET Core 的 Middleware - ASP.NET Core 信息分享

相關文章:?

  • Middleware的藝術

  • dotnetCore增加MiddleWare的Run,Use Map MapThen四個擴展方法

  • ASP.NET Core提供模塊化Middleware組件

  • ASP.NET Core 開發(fā)-中間件(Middleware)

  • [ASP.NET Core] Static File Middleware

  • 用Middleware給ASP.NET Core Web API添加自己的授權驗證


原文地址:http://www.cnblogs.com/clark159/p/5974280.html


.NET社區(qū)新聞,深度好文,微信中搜索dotNET跨平臺或掃描二維碼關注

贊賞

總結(jié)

以上是生活随笔為你收集整理的[ASP.NET Core] Middleware的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。