<i id='q88v8'><tr id='q88v8'><dt id='q88v8'><q id='q88v8'><span id='q88v8'><b id='q88v8'><form id='q88v8'><ins id='q88v8'></ins><ul id='q88v8'></ul><sub id='q88v8'></sub></form><legend id='q88v8'></legend><bdo id='q88v8'><pre id='q88v8'><center id='q88v8'></center></pre></bdo></b><th id='q88v8'></th></span></q></dt></tr></i><div id='q88v8'><tfoot id='q88v8'></tfoot><dl id='q88v8'><fieldset id='q88v8'></fieldset></dl></div>

      • <bdo id='q88v8'></bdo><ul id='q88v8'></ul>
    1. <tfoot id='q88v8'></tfoot>
      1. <legend id='q88v8'><style id='q88v8'><dir id='q88v8'><q id='q88v8'></q></dir></style></legend>

        <small id='q88v8'></small><noframes id='q88v8'>

        如何在 MVC6 中正确注入 HttpContext

        时间:2023-06-08

        <i id='XARMA'><tr id='XARMA'><dt id='XARMA'><q id='XARMA'><span id='XARMA'><b id='XARMA'><form id='XARMA'><ins id='XARMA'></ins><ul id='XARMA'></ul><sub id='XARMA'></sub></form><legend id='XARMA'></legend><bdo id='XARMA'><pre id='XARMA'><center id='XARMA'></center></pre></bdo></b><th id='XARMA'></th></span></q></dt></tr></i><div id='XARMA'><tfoot id='XARMA'></tfoot><dl id='XARMA'><fieldset id='XARMA'></fieldset></dl></div>

          <legend id='XARMA'><style id='XARMA'><dir id='XARMA'><q id='XARMA'></q></dir></style></legend>
            <tfoot id='XARMA'></tfoot>
                <tbody id='XARMA'></tbody>

                <bdo id='XARMA'></bdo><ul id='XARMA'></ul>

                <small id='XARMA'></small><noframes id='XARMA'>

                  本文介绍了如何在 MVC6 中正确注入 HttpContext的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我的 API 中的数据服务层需要 httpcontext 中的请求信息,我阅读了此 问题,他们说我应该使用 ActionContext 而不是 HttpContext.Current(在 MVC6 中停止).

                  My data service layer in my API required information that are of the request in the httpcontext, I read this question and they said that I should used the ActionContext instead of HttpContext.Current (discontinue in MVC6).

                  第一种方法是通过覆盖这个方法来设置控制器内部的数据:

                  The first way is to set the data inside the controller by overriding this method:

                  public void OnActionExecuting(ActionExecutingContext context)
                  {
                      var routeData = context.RouteData;
                      var httpContext = context.HttpContext;
                      ...
                  }
                  

                  或者通过注入服务层来使用DI

                  Or using DI by injecting into the service layer

                  public MyService(IContextAccessor<ActionContext> contextAccessor)
                  {
                      _httpContext = contextAccessor.Value.HttpContext;
                      _routeData = contextAccessor.Value.RouteData;
                  }
                  

                  但我不确定下面列出的两行代码是否是执行 DI 的正确方法

                  but I'm not sure with of the both line of code listed below is correct way to do the DI

                  services.AddTransient<IContextAccessor<ActionContext>,ContextAccessor>();
                  services.AddTransient<IContextAccessor<ActionContext>>();
                  

                  当我这样做时,我得到了这个错误.

                  when I do this I get this error.

                  尝试激活时无法解析类型Microsoft.AspNet.Mvc.ActionContext"的服务

                  Unable to resolve service for type 'Microsoft.AspNet.Mvc.ActionContext' while attempting to activate

                  更新project.json 网络项目

                  Update project.json web project

                  "DIMultiTenan.Infrastructure": "",
                  "DIMultiTenan.MongoImplementation": "", 
                  "Microsoft.AspNet.Server.IIS": "1.0.0-beta3",
                  "Microsoft.AspNet.Mvc": "6.0.0-beta3",
                  "Microsoft.AspNet.StaticFiles": "1.0.0-beta3",
                  "Microsoft.AspNet.Server.WebListener": "1.0.0-beta3"
                  

                  推荐答案

                  如果你试图访问 HttpContext,那么你可以使用 IHttpContextAccessor 来达到这个目的.

                  If you are trying to access HttpContext, then you can use IHttpContextAccessor for this purpose.

                  例子:

                  services.AddTransient<QueryValueService>();
                  

                  <小时>

                  public class QueryValueService
                  {
                      private readonly IHttpContextAccessor _accessor;
                  
                      public QueryValueService(IHttpContextAccessor httpContextAccessor)
                      {
                          _accessor = httpContextAccessor;
                      }
                  
                      public string GetValue()
                      {
                          return _accessor.HttpContext.Request.Query["value"];
                      }
                  }
                  

                  请注意,在上面的示例中,QueryValueService 应仅注册为 TransientScoped 而不是 SingletonHttpContext 是基于每个请求的...

                  Note that in the above example QueryValueService should be registered only as Transient or Scoped and not Singleton as HttpContext is per-request based...

                  这篇关于如何在 MVC6 中正确注入 HttpContext的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  上一篇:在 ASP.NET Core MVC 中自定义响应序列化 下一篇:ASP.NET Core 依赖解析器

                  相关文章

                  <legend id='6ASTC'><style id='6ASTC'><dir id='6ASTC'><q id='6ASTC'></q></dir></style></legend>

                    <tfoot id='6ASTC'></tfoot>
                  1. <i id='6ASTC'><tr id='6ASTC'><dt id='6ASTC'><q id='6ASTC'><span id='6ASTC'><b id='6ASTC'><form id='6ASTC'><ins id='6ASTC'></ins><ul id='6ASTC'></ul><sub id='6ASTC'></sub></form><legend id='6ASTC'></legend><bdo id='6ASTC'><pre id='6ASTC'><center id='6ASTC'></center></pre></bdo></b><th id='6ASTC'></th></span></q></dt></tr></i><div id='6ASTC'><tfoot id='6ASTC'></tfoot><dl id='6ASTC'><fieldset id='6ASTC'></fieldset></dl></div>

                    <small id='6ASTC'></small><noframes id='6ASTC'>

                      • <bdo id='6ASTC'></bdo><ul id='6ASTC'></ul>