18

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();
spottedmahn
  • 14,823
  • 13
  • 108
  • 178
Ahad Porkar
  • 1,666
  • 2
  • 33
  • 68

3 Answers3

23

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

Jeremy
  • 1,953
  • 1
  • 15
  • 18
fran.tonkovic
  • 369
  • 2
  • 6
  • 1
    It provides a possible solution to Andrey Markovs' problem, as it was the solution for my problem that brought me here in the first place. I don't see how deleting my comment and downvoting it was productive - is it really better for this question to be unanswered?! – fran.tonkovic May 19 '16 at 09:54
  • @ManoDestra How does this not provide an answer? – Undo May 19 '16 at 14:46
  • 1
    "I replaced it with ControllerContext" - replaced what exactly? There's no code in this question nor sufficient explanation of how to resolve the issue. With some editing, it could be improved though :) – ManoDestra May 19 '16 at 19:44
  • 1
    This is the correct answer. It seemed clear, but "it" refers to the ActionContext property indicated in the question. So: replace ActionContext with ControllerContext in RC2. – Jeremy May 25 '16 at 12:35
  • 2
    It works because `ControllerContext` is a subclass of `ActionContext`. – Mariusz Jamro Jan 28 '19 at 20:48
23

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.

Mariusz Jamro
  • 30,615
  • 24
  • 120
  • 162
  • This is strange. I just upgraded my codebase to .net core 3.1 and after that, I started getting null for IActionContextAcessor (I had no issues with .net core 2.2). The issue was gone after I explicitly added the line you mentioned here. I wonder why this has anything to do with .net core 3.1.... Thanks. – hamiltonjose Apr 16 '20 at 23:45
  • This actually helped me resolve my other problem of passing the actionContextAccessor to my Service Layer – Nathan Aug 04 '21 at 19:39
3

In .NET core 3.1 at least, ControllerBase has a property Url that works as a UrlHelper so you don't have to make one yourself. https://stackoverflow.com/a/52537244/4172413

Brian Davis
  • 745
  • 1
  • 11
  • 14