Is ApiController deprecated in .NET Core

C#asp.net Mvcasp.net Web-Apiasp.net Core.Net Core

C# Problem Overview


Is it true that "ApiController will get deprecated in .NET Core"? Asking since I'm planning to use it in new projects.

C# Solutions


Solution 1 - C#

Update ASP.NET Core 2.1

Since ASP.NET Core 2.1 a new set of types is available to create Web API controllers. You can annotate your controllers with the [ApiController] attribute which enables a few new features such as automatic model state validation and binding source parameter inference. See the docs for more information: https://docs.microsoft.com/en-us/aspnet/core/web-api/index?view=aspnetcore-2.1#annotate-class-with-apicontrollerattribute.


There is indeed no particular ApiController class anymore since MVC and WebAPI have been merged in ASP.NET Core. However, the Controller class of MVC brings in a bunch of features you probably won't need when developing just a Web API, such as a views and model binding.

You've got two options if you want something different:

Use the ControllerBase class in the Microsoft.AspNetCore.Mvc.Core package.

Or

Create your ApiController base class. The key here is to add the [ActionContext] attribute which injects the current ActionContext instance into the property:

[Controller]
public abstract class ApiController
{
    [ActionContext]
    public ActionContext ActionContext { get; set; }
}

Also, add the [Controller] attribute to the class to mark it as a controller for the MVC controller discovery.

See more details in my “Web API in MVC 6” blogpost.

Solution 2 - C#

The [ApiController] attribute actually got added back in ASP.NET Core version 2.1.

Features coupled with the attribute are:

  • Validation errors automatically trigger an HTTP 400 response.
  • No more need to define [FromBody], [FromRoute], ... attributes explicitly

Links to the docs:

Update

There is also the baseclass ControllerBase for controllers to inherit from which is suited for api-controllers because it ommits all view-related functionality.

Solution 3 - C#

In ASP.NET core uses terms and concepts known from ASP.NET MVC and ASP.NET WepAPI. But basically it is a complete new framework. Therefore there are several concepts or base classes that we can simply forget.

ASP.NET MVC and ASP.NET WebApi are two coexisting but different frameworks and therefore a destinction has to be made to specify a controller as a WebApi Controller by using the ApiController as base class.

In ASP.NET Core this is simply not necessary anymore. The Controller base class can be used for actions that return HTML from Razor Views or JSON (with output formatters XML and other formats are possible as well). You don't even need the Controller base class. It is even possible to use a "Plain Old C# Object" as Controller without inheritence.

Below is an example of a Demo-Controller to outline, that even though the ApiController is not there, the structural approach to deliver data to the client is similar.

public class DemoController : Controller
{       
     public async Task<IActionResult> Action()
     {
         var model = await _someService.GetPreciousData();
         return Ok(model);
     }
 }

Solution 4 - C#

As others mentioned, ASP.NET Core is a complete new webstack that's not compatible with the old ASP.NET MVC webstack. This is explicitly reflected in it's name and versioning!

ASP.NET Core and ASP.NET Core MVC have the version 1.0.0 to make this incompatibility very clear.

ASP.NET Core merged the MVC and WebApi into one single Api just called.

And here's the thing you may have been looking for:

If you are migrating from a previous ASP.NET MVC or ASP.NET WebApi application, you may want to import the Microsoft.AspNetCore.Mvc.WebApiCompatShim package which provides some compatibility types which makes migrations easier from the previous versions. Among them is the ApiController class and certain attributes that were removed in the new webstack Api.

However, please note that this is only there to help you with migrating existing applications. When you create a new application you shouldn't use this compatibility shim and just use the new stuff.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionVijiView Question on Stackoverflow
Solution 1 - C#Henk MollemaView Answer on Stackoverflow
Solution 2 - C#RiscieView Answer on Stackoverflow
Solution 3 - C#Ralf BönningView Answer on Stackoverflow
Solution 4 - C#TsengView Answer on Stackoverflow