在ASP.NET Core5.0中訪問HttpContext的方法步驟
ASP.NET Core 應(yīng)用通過 IHttpContextAccessor 接口及其默認(rèn)實(shí)現(xiàn) HttpContextAccessor 訪問 HttpContext。 只有在需要訪問服務(wù)內(nèi)的 HttpContext 時(shí),才有必要使用 IHttpContextAccessor。
通過 Razor Pages 使用 HttpContext
Razor Pages PageModel 公開 HttpContext 屬性:
public class AboutModel : PageModel { public string Message { get; set; } public void OnGet() { Message = HttpContext.Request.PathBase; } }
通過 Razor 視圖使用 HttpContext
Razor 視圖通過視圖上的 RazorPage.Context 屬性直接公開 HttpContext。 下面的示例使用 Windows 身份驗(yàn)證檢索 Intranet 應(yīng)用中的當(dāng)前用戶名:
@{ var username = Context.User.Identity.Name; ... }
通過控制器使用 HttpContext
控制器公開 ControllerBase.HttpContext 屬性:
public class HomeController : Controller { public IActionResult About() { var pathBase = HttpContext.Request.PathBase; ... return View(); } }
通過中間件使用 HttpContext
使用自定義中間件組件時(shí),HttpContext 傳遞到 Invoke 或 InvokeAsync 方法,在中間件配置后可供訪問:
public class MyCustomMiddleware { public Task InvokeAsync(HttpContext context) { ... } }
通過自定義組件使用 HttpContext
對(duì)于需要訪問 HttpContext 的其他框架和自定義組件,建議使用內(nèi)置的依賴項(xiàng)注入容器來注冊(cè)依賴項(xiàng)。 依賴項(xiàng)注入容器向任意類提供 IHttpContextAccessor,以供類在自己的構(gòu)造函數(shù)中將它聲明為依賴項(xiàng):
public void ConfigureServices(IServiceCollection services) { services.AddControllersWithViews(); services.AddHttpContextAccessor(); services.AddTransient<IUserRepository, UserRepository>(); }
如下示例中:
- UserRepository 聲明自己對(duì) IHttpContextAccessor 的依賴。
- 當(dāng)依賴項(xiàng)注入容器解析依賴鏈并創(chuàng)建 UserRepository 實(shí)例時(shí),就會(huì)注入依賴 項(xiàng)。
public class UserRepository : IUserRepository { private readonly IHttpContextAccessor _httpContextAccessor; public UserRepository(IHttpContextAccessor httpContextAccessor) { _httpContextAccessor = httpContextAccessor; } public void LogCurrentUser() { var username = _httpContextAccessor.HttpContext.User.Identity.Name; service.LogAccessRequest(username); } }
從后臺(tái)線程訪問 HttpContext
HttpContext 不是線程安全型。 在處理請(qǐng)求之外讀取或?qū)懭?HttpContext 的屬性可能會(huì)導(dǎo)致 NullReferenceException。
要使用 HttpContext 數(shù)據(jù)安全地執(zhí)行后臺(tái)工作,請(qǐng)執(zhí)行以下操作:
- 在請(qǐng)求處理過程中復(fù)制所需的數(shù)據(jù)。
- 將復(fù)制的數(shù)據(jù)傳遞給后臺(tái)任務(wù)。
要避免不安全代碼,請(qǐng)勿將 HttpContext 傳遞給執(zhí)行后臺(tái)工作的方法。 而是傳遞所需要的數(shù)據(jù)。 在以下示例中,調(diào)用 SendEmailCore,開始發(fā)送電子郵件。 將 correlationId 傳遞到 SendEmailCore,而不是 HttpContext。 代碼執(zhí)行不會(huì)等待 SendEmailCore 完成:
public class EmailController : Controller { public IActionResult SendEmail(string email) { var correlationId = HttpContext.Request.Headers["x-correlation-id"].ToString(); _ = SendEmailCore(correlationId); return View(); } private async Task SendEmailCore(string correlationId) { ... } }
到此這篇關(guān)于在ASP.NET Core5.0中訪問HttpContext的方法步驟的文章就介紹到這了,更多相關(guān)ASP.NET Core 訪問 HttpContext內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
ASP.NET MVC 3實(shí)現(xiàn)訪問統(tǒng)計(jì)系統(tǒng)
我們將介紹用ASP.NET MVC 3實(shí)現(xiàn)一個(gè)訪問統(tǒng)計(jì)系統(tǒng),包括統(tǒng)計(jì)代碼和Cookie等方面,希望對(duì)大家有所幫助。2015-10-10ASP.NET?MVC創(chuàng)建XML文件并實(shí)現(xiàn)元素增刪改
這篇文章介紹了ASP.NET?MVC創(chuàng)建XML文件并實(shí)現(xiàn)元素增刪改的方法,文中通過示例代碼介紹的非常詳細(xì)。對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2022-07-07Community Server專題一:概述Community Server
Community Server專題一:概述Community Server...2007-03-03.NET微信開發(fā)之PC 端微信掃碼注冊(cè)和登錄功能實(shí)現(xiàn)
這篇文章主要介紹了.NET微信開發(fā)之PC 端微信掃碼注冊(cè)和登錄功能實(shí)現(xiàn)的相關(guān)資料,非常不錯(cuò),具有參考借鑒價(jià)值,需要的朋友可以參考下2016-09-09