4

I have migrated the project from Dot.Net-Framework 4.7.2 to Asp.Net-Core 3.1. I want to change all property of ApiController Class in the WebApi.

public static HttpActionContext CreateActionContext(HttpControllerContext controllerContext = null, 
                                                    HttpActionDescriptor actionDescriptor = null)
{
    HttpControllerContext context = controllerContext ?? CreateControllerContext();
    HttpActionDescriptor descriptor = actionDescriptor ?? CreateActionDescriptor();
    descriptor.ControllerDescriptor = context.ControllerDescriptor;
    return new HttpActionContext(context, descriptor);
}

For System.Web.Http, I followed the below link. When I used, it's working, but don't know whether is this right method and below one is not supported in MAC

System.Web.Http missing in .net Core 2.1

Kindly provide any alternative or better solution

Karthic G
  • 1,162
  • 1
  • 14
  • 31
  • 1
    What exactly are you trying to do? That is probably a better way to get an answer here - there may not be code that is directly translatable. – DavidG Sep 21 '20 at 15:12
  • want to know what is equivalent to HttpControllerContext Class in Asp.Net Core 3.1.If NetFramework there are so many properties under ApiController Class.so get to know property equivalent in ControllerBase class in Core 3.1 – Karthic G Sep 21 '20 at 15:21
  • Yes, your question already says that - but it doesn't help. – DavidG Sep 21 '20 at 15:23
  • @KarthicG Where are you accessing the context exactly? Describe what are trying to do. – HMZ Sep 21 '20 at 15:24

1 Answers1

2

In controllers and actions you can access the HttpContext using the property HttpContext by inheriting from the base class ControllerBase

While if you needed the HttpContext in other components you have to use dependency injection like this:

public void ConfigureServices(IServiceCollection services)
{
     services.AddHttpContextAccessor();
}

Then by injecting it in the constructor:

public class SomeClass
{
    private readonly IHttpContextAccessor _httpContextAccessor;

    public SomeClass(IHttpContextAccessor httpContextAccessor)
    {
        _httpContextAccessor = httpContextAccessor;
    }
}

MsDocs

HMZ
  • 2,949
  • 1
  • 19
  • 30