婷婷久久综合九色综合,欧美成色婷婷在线观看视频,偷窥视频一区,欧美日本一道道一区二区

<tt id="bu9ss"></tt>
  • <span id="bu9ss"></span>
  • <pre id="bu9ss"><tt id="bu9ss"></tt></pre>
    <label id="bu9ss"></label>

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

    在ASP.NETCore5.0中訪問(wèn)HttpContext的方法步驟

     2020-11-20 14:37  來(lái)源: 腳本之家   我來(lái)投稿 撤稿糾錯(cuò)

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

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

    ASP.NET Core 應(yīng)用通過(guò) IHttpContextAccessor 接口及其默認(rèn)實(shí)現(xiàn) HttpContextAccessor 訪問(wèn) HttpContext。 只有在需要訪問(wèn)服務(wù)內(nèi)的 HttpContext 時(shí),才有必要使用 IHttpContextAccessor。

    通過(guò) Razor Pages 使用 HttpContext

    Razor Pages PageModel 公開(kāi) HttpContext 屬性:

    public class AboutModel : PageModel
    {
      public string Message { get; set; }


      public void OnGet()
      {
        Message = HttpContext.Request.PathBase;
      }
    }

    通過(guò) Razor 視圖使用 HttpContext

    Razor 視圖通過(guò)視圖上的 RazorPage.Context 屬性直接公開(kāi) HttpContext。 下面的示例使用 Windows 身份驗(yàn)證檢索 Intranet 應(yīng)用中的當(dāng)前用戶名:

    @{
      var username = Context.User.Identity.Name;
     
      ...
    }

    通過(guò)控制器使用 HttpContext

    控制器公開(kāi) ControllerBase.HttpContext 屬性:

    public class HomeController : Controller
    {
      public IActionResult About()
      {
        var pathBase = HttpContext.Request.PathBase;


        ...


        return View();
      }
    }

    通過(guò)中間件使用 HttpContext

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

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

    通過(guò)自定義組件使用 HttpContext

    對(duì)于需要訪問(wèn) HttpContext 的其他框架和自定義組件,建議使用內(nèi)置的依賴項(xiàng)注入容器來(lái)注冊(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)線程訪問(wèn) HttpContext

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

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

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

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

    要避免不安全代碼,請(qǐng)勿將 HttpContext 傳遞給執(zhí)行后臺(tái)工作的方法。 而是傳遞所需要的數(shù)據(jù)。 在以下示例中,調(diào)用 SendEmailCore,開(kāi)始發(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中訪問(wèn)HttpContext的方法步驟的文章就介紹到這了,更多相關(guān)ASP.NET Core 訪問(wèn) HttpContext內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

    來(lái)源:腳本之家

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

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

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

    相關(guān)文章

    熱門(mén)排行

    信息推薦