乱码乱a∨中文字幕,在线免费激情视频,亚洲欧美久久夜夜潮,国产在线网址

  1. <sub id="hjl7n"></sub>

    1. <sub id="hjl7n"></sub>

      <legend id="hjl7n"></legend>

      當(dāng)前位置:首頁 >  站長 >  編程技術(shù) >  正文

      在ASP.NETCore5.0中訪問HttpContext的方法步驟

       2020-11-20 14:37  來源: 腳本之家   我來投稿 撤稿糾錯

        阿里云優(yōu)惠券 先領(lǐng)券再下單

      這篇文章主要介紹了在ASP.NET Core5.0中訪問HttpContext的方法步驟,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧

      ASP.NET Core 應(yīng)用通過 IHttpContextAccessor 接口及其默認(rèn)實現(xiàn) HttpContextAccessor 訪問 HttpContext。 只有在需要訪問服務(wù)內(nèi)的 HttpContext 時,才有必要使用 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 身份驗證檢索 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

      使用自定義中間件組件時,HttpContext 傳遞到 Invoke 或 InvokeAsync 方法,在中間件配置后可供訪問:

      public class MyCustomMiddleware
      {
        public Task InvokeAsync(HttpContext context)
        {
          ...
        }
      }

      通過自定義組件使用 HttpContext

      對于需要訪問 HttpContext 的其他框架和自定義組件,建議使用內(nèi)置的依賴項注入容器來注冊依賴項。 依賴項注入容器向任意類提供 IHttpContextAccessor,以供類在自己的構(gòu)造函數(shù)中將它聲明為依賴項:

      public void ConfigureServices(IServiceCollection services)
      {
         services.AddControllersWithViews();
         services.AddHttpContextAccessor();
         services.AddTransient<IUserRepository, UserRepository>();
      }

      如下示例中:

      UserRepository 聲明自己對 IHttpContextAccessor 的依賴。

      當(dāng)依賴項注入容器解析依賴鏈并創(chuàng)建 UserRepository 實例時,就會注入依賴項。

      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);
        }
      }

      從后臺線程訪問 HttpContext

      HttpContext 不是線程安全型。 在處理請求之外讀取或?qū)懭?HttpContext 的屬性可能會導(dǎo)致 NullReferenceException。

      要使用 HttpContext 數(shù)據(jù)安全地執(zhí)行后臺工作,請執(zhí)行以下操作:

      在請求處理過程中復(fù)制所需的數(shù)據(jù)。

      將復(fù)制的數(shù)據(jù)傳遞給后臺任務(wù)。

      要避免不安全代碼,請勿將 HttpContext 傳遞給執(zhí)行后臺工作的方法。 而是傳遞所需要的數(shù)據(jù)。 在以下示例中,調(diào)用 SendEmailCore,開始發(fā)送電子郵件。 將 correlationId 傳遞到 SendEmailCore,而不是 HttpContext。 代碼執(zhí)行不會等待 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)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

      來源:腳本之家

      鏈接:https://www.jb51.net/article/199590.htm

      申請創(chuàng)業(yè)報道,分享創(chuàng)業(yè)好點子。點擊此處,共同探討創(chuàng)業(yè)新機(jī)遇!

      相關(guān)標(biāo)簽
      asp.net

      相關(guān)文章

      熱門排行

      信息推薦