您好,登錄后才能下訂單哦!
本篇內(nèi)容主要講解“ASP.NET Core的Middleware怎么使用”,感興趣的朋友不妨來看看。本文介紹的方法操作簡(jiǎn)單快捷,實(shí)用性強(qiáng)。下面就讓小編來帶大家學(xué)習(xí)“ASP.NET Core的Middleware怎么使用”吧!
在ASP.NET Core里,每個(gè)從「瀏覽器傳入」的HTTP Request封包,會(huì)被系統(tǒng)封裝為「HttpRequest對(duì)象」,并且配置默認(rèn)的HttpResponse對(duì)象、Session對(duì)象、ClaimsPrincipal對(duì)象...等等物件。接著將這些對(duì)象,封裝成為一個(gè)「HttpContext對(duì)象」,用來提供ASP.NET Core后續(xù)使用。
ASP.NET Core在收到HttpContext之后,會(huì)把它交給一個(gè)「Pipeline」去處理。這個(gè)Pipeline里面配置很多「Middleware」。系統(tǒng)會(huì)將HttpContext,依序傳遞給Pipeline里的Middleware去處理。每個(gè)Middleware會(huì)依照自己內(nèi)部的程序邏輯,來運(yùn)算處理HttpContext,并且變更HttpContext所封裝的對(duì)象內(nèi)容。
ASP.NET Core在收到經(jīng)由Middleware處理完畢的HttpContext之后,就會(huì)取出其中所封裝的HttpResponse對(duì)象。然后依照這個(gè)HttpResponse對(duì)象,來建立從「服務(wù)器回傳」的HTTP Response封包內(nèi)容。
ASP.NET Core經(jīng)由上述的系統(tǒng)結(jié)構(gòu),完成HTTP Request封包輸入、HTTP Response封包輸出的工作流程。
在這個(gè)范例里,Middleware透過實(shí)做Invoke方法,來提供自己所封裝的程序邏輯。
public class HelloWorldMiddleware { // Fields private readonly RequestDelegate _next; // Constructors public HelloWorldMiddleware(RequestDelegate next) { _next = next; } // Methods public Task Invoke(HttpContext context) { // Response context.Response.WriteAsync("Hello World!"); // Return return Task.CompletedTask; } }
在實(shí)做Middleware.Invoke方法的時(shí)候,開發(fā)人員可以透過HttpContext.Request,來取得從「瀏覽器傳入」的HTTP Request封包內(nèi)容。在下列的范例程序代碼里,就是透過HttpContext.Request的Path、QueryString兩個(gè)屬性,來分別取得HTTP Request封包的URL Path與QueryString。
public class HelloWorldMiddleware { // Fields private readonly RequestDelegate _next; // Constructors public HelloWorldMiddleware(RequestDelegate next) { _next = next; } // Methods public Task Invoke(HttpContext context) { // Request string path = context.Request.Path.ToString(); string queryString = context.Request.QueryString.ToString(); string message = string.Format("path={0}, queryString={1}", path, queryString); // Response context.Response.WriteAsync(message); // Return return Task.CompletedTask; } }
同樣在實(shí)做Middleware.Invoke方法的時(shí)候,開發(fā)人員可以透過HttpContext.Response,來設(shè)定從「服務(wù)器回傳」的HTTP Response封包內(nèi)容。在下列的范例程序代碼里,就是透過HttpContext.Response的WriteAsync方法、StatusCode屬性,來分別設(shè)定HTTP Response封包的Content與StatusCode。
public class HelloWorldMiddleware { // Fields private readonly RequestDelegate _next; // Constructors public HelloWorldMiddleware(RequestDelegate next) { _next = next; } // Methods public Task Invoke(HttpContext context) { // Response context.Response.StatusCode = 404; context.Response.WriteAsync("Not Found"); // Return return Task.CompletedTask; } }
而在實(shí)做Middleware.Invoke方法的時(shí)候,如果程序代碼里發(fā)生了預(yù)期之外的Exception。ASP.NET Core預(yù)設(shè)會(huì)使用「500 Internal Server Error」,這個(gè)StatusCode來通報(bào)系統(tǒng)內(nèi)部發(fā)生異常。 在下列的范例程序代碼里,就是直接拋出一個(gè)例外錯(cuò)誤,交由ASP.NET Core的錯(cuò)誤處理機(jī)制去處理。
public class HelloWorldMiddleware { // Fields private readonly RequestDelegate _next; // Constructors public HelloWorldMiddleware(RequestDelegate next) { _next = next; } // Methods public Task Invoke(HttpContext context) { // Exception throw new Exception(); // Return return Task.CompletedTask; } }
建立Middleware的時(shí)候,開發(fā)人員可以透過建構(gòu)子所傳入的RequestDelegate,來參考到Pipeline里的下一個(gè)Middleware。透過調(diào)用RequestDelegate,就可以調(diào)用Pipeline里的下一個(gè)Middleware的Invoke方法。在下列的范例程序代碼里,就是透過調(diào)用RequestDelegate,來調(diào)用Pipeline里的下一個(gè)Middleware的Invoke方法,藉此串接其他Middleware的程序邏輯。
public class HelloWorldMiddleware { // Fields private readonly RequestDelegate _next; // Constructors public HelloWorldMiddleware(RequestDelegate next) { _next = next; } // Methods public async Task Invoke(HttpContext context) { // Do Something 01 //.... // Next await _next.Invoke(context); // Do Something 02 // ... } }
到此,相信大家對(duì)“ASP.NET Core的Middleware怎么使用”有了更深的了解,不妨來實(shí)際操作一番吧!這里是億速云網(wǎng)站,更多相關(guān)內(nèi)容可以進(jìn)入相關(guān)頻道進(jìn)行查詢,關(guān)注我們,繼續(xù)學(xué)習(xí)!
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。