如何正确地MVC6注入的HttpContext [英] How properly inject HttpContext in MVC6

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

问题描述

在那些在HttpContext的请求我的API所需的信息数据我服务层,我读到这问题,他们说我应该用ActionContext中代替HttpContext.Current(停止在MVC6)。

第一种方式是通过重写此方法来设置控制器内部的数据:

 公共无效OnActionExecuting(ActionExecutingContext上下文)
{
    VAR的RouteData = context.RouteData;
    VAR的HttpContext = context.HttpContext;
    ...
}

或使用DI注入到服务层

 公共则将MyService(IContextAccessor<&ActionContext的GT; contextAccessor)
{
    _httpContext = contextAccessor.Value.HttpContext;
    _routeData = contextAccessor.Value.RouteData;
}

但我不知道下面列出code的两个线是做DI正确的方法

  services.AddTransient< IContextAccessor<&ActionContext的GT;,&ContextAccessor ​​GT;();
services.AddTransient< IContextAccessor<&ActionContext的GT;>();

我这样做时,我得到这个错误。

无法为类型解析服务Microsoft.AspNet.Mvc.ActionContext,而试图激活

更新
project.json Web项目

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


解决方案

如果您尝试访问的HttpContext ,那么你可以使用 IHttpContextAccessor ​​用于这一目的。

例如:

  services.AddTransient< QueryValueService>();


 公共类QueryValueService
{
    私人只读IHttpContextAccessor ​​_accessor;    公共QueryValueService(IHttpContextAccessor ​​httpContextAccessor)
    {
        _accessor ​​= httpContextAccessor;
    }    公共字符串的GetValue()
    {
        返回_accessor.HttpContext.Request.Query [值];
    }
}

请注意,在上面的例子中 QueryValueService 只应注册为瞬态作用域,而不是辛格尔顿为HttpContext的是每个请求的基础...

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

Or using DI by injecting into the service layer

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

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.

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

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"

解决方案

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

Example:

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"];
    }
}

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的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆