[ASP.NET Core] Middleware
前言
本篇文章介紹ASP.NET Core里,用來處理HTTP封包的Middleware,為自己留個紀錄也希望能幫助到有需要的開發人員。
ASP.NET Core官網
結構
在ASP.NET Core里,每個從「瀏覽器傳入」的HTTP Request封包,會被系統封裝為「HttpRequest對象」,并且配置默認的HttpResponse對象、Session對象、ClaimsPrincipal對象...等等物件。接著將這些對象,封裝成為一個「HttpContext對象」,用來提供ASP.NET Core后續使用。
frameborder="0" scrolling="no" style="border-width: initial; border-style: none; width: 715px; height: 521px;">
ASP.NET Core在收到HttpContext之后,會把它交給一個「Pipeline」去處理。這個Pipeline里面配置很多「Middleware」。系統會將HttpContext,依序傳遞給Pipeline里的Middleware去處理。每個Middleware會依照自己內部的程序邏輯,來運算處理HttpContext,并且變更HttpContext所封裝的對象內容。
frameborder="0" scrolling="no" style="border-width: initial; border-style: none; width: 715px; height: 521px;">
ASP.NET Core在收到經由Middleware處理完畢的HttpContext之后,就會取出其中所封裝的HttpResponse對象。然后依照這個HttpResponse對象,來建立從「服務器回傳」的HTTP Response封包內容。
frameborder="0" scrolling="no" style="border-width: initial; border-style: none; width: 715px; height: 519px;">
ASP.NET Core經由上述的系統結構,完成HTTP Request封包輸入、HTTP Response封包輸出的工作流程。
frameborder="0" scrolling="no" style="border-width: initial; border-style: none; width: 715px; height: 519px;">
開發
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方法的時候,開發人員可以透過HttpContext.Request,來取得從「瀏覽器傳入」的HTTP Request封包內容。在下列的范例程序代碼里,就是透過HttpContext.Request的Path、QueryString兩個屬性,來分別取得HTTP Request封包的URL路徑與QueryString內容。
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方法的時候,開發人員可以透過HttpContext.Response,來設定從「服務器回傳」的HTTP Response封包內容。在下列的范例程序代碼里,就是透過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方法的時候,如果程序代碼里發生了預期之外的Exception。ASP.NET Core預設會使用「500 Internal Server Error」,這個StatusCode來通報系統內部發生異常。 在下列的范例程序代碼里,就是直接拋出一個例外錯誤,交由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的時候,開發人員可以透過建構子所傳入的RequestDelegate,來參考到Pipeline里的下一個Middleware。透過調用RequestDelegate,就可以調用Pipeline里的下一個Middleware的Invoke方法。在下列的范例程序代碼里,就是透過調用RequestDelegate,來調用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 開發-中間件(Middleware)
[ASP.NET Core] Static File Middleware
用Middleware給ASP.NET Core Web API添加自己的授權驗證
原文地址:http://www.cnblogs.com/clark159/p/5974280.html
.NET社區新聞,深度好文,微信中搜索dotNET跨平臺或掃描二維碼關注
贊賞
總結
以上是生活随笔為你收集整理的[ASP.NET Core] Middleware的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 使用 Angular
- 下一篇: 跨平台的.NET邮件协议MailKit组