ActionContext gone in Microsoft.AspNetCore.Mvc.Con

2020-08-19 05:06发布

I cant find ActionContext in Microsoft.AspNetCore.Mvc.Controller

after i changed my Version to AspNetCore 1.0.0-preview1

this is Controller class (after Change):

enter image description here

And from "Microsoft.AspNet.Mvc" before change :

enter image description here

and code from old method before update :

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

2条回答
欢心
2楼-- · 2020-08-19 05:40

I replaced ActionContext with ControllerContext, and it works for me. I don't know if it's an official migration step, though.

查看更多
够拽才男人
3楼-- · 2020-08-19 05:45

You can inject IActionContextAccessor to your class. It provides access to the action context.

services.AddSingleton<IActionContextAccessor, ActionContextAccessor>();

Use it:

private readonly IActionContextAccessor actionContextAccessor

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

See this issue.

查看更多
登录 后发表回答