ActionContext在Microsoft.AspNetCore.Mvc.Controller中消失了

我无法在Microsoft.AspNetCore.Mvc.Controller中找到ActionContext

之后我将我的版本更改为AspNetCore 1.0.0-preview1

这是Controller类(更改后):

在此处输入图像描述

并且在更改之前从“ Microsoft.AspNet.Mvc ”:

在此处输入图像描述

更新前的旧方法代码:

this.Agent = ControllerInfo.Request.Headers["User-Agent"]; this.IP = ControllerInfo.HttpContext.Features.Get()?.LocalIpAddress?.ToString(); this.RemoteIP = ControllerInfo.HttpContext.Features.Get()?.RemoteIpAddress.ToString(); this.Refrence = ControllerInfo.ActionContext.RouteData.Values["controller"].ToString() + "/" + ControllerInfo.ActionContext.RouteData.Values["action"].ToString(); 

我用ControllerContext替换了ActionContext ,它对我有用 。 不过,我不知道这是否是官方迁移步骤。

您可以将IActionContextAccessor注入您的类。 它提供对操作上下文的访问。

 services.AddSingleton(); 

用它:

 private readonly IActionContextAccessor actionContextAccessor public FooController(IActionContextAccessor actionContextAccessor) { this.actionContextAccessor = actionContextAccessor; } 

看到这个问题 。