Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is ApiController deprecated in .NET Core

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

like image 264
Viji Avatar asked Jul 29 '16 21:07

Viji


People also ask

Is ApiController deprecated?

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.

What is deprecated in .NET Core?

NET Framework will be deprecated. This means you can only use . NET Framework as long as your operating systems (for example Windows Server 2019) still supports it. And with Microsoft shortening its support lifecycles, your operating system end-of-life will come sooner than you may think.

What is ApiController in .NET Core?

ApiController attribute The [ApiController] attribute can be applied to a controller class to enable the following opinionated, API-specific behaviors: Attribute routing requirement. Automatic HTTP 400 responses. Binding source parameter inference.

What is the difference between ApiController and controller?

They work similarly in Web API, but controllers in Web API derive from the ApiController class instead of Controller class. The first major difference you will notice is that actions on Web API controllers do not return views, they return data. ApiControllers are specialized in returning data.


2 Answers

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.

like image 51
Henk Mollema Avatar answered Sep 20 '22 03:09

Henk Mollema


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:

  • https://docs.microsoft.com/en-us/aspnet/core/aspnetcore-2.1?view=aspnetcore-2.1#apicontroller-actionresult
  • https://docs.microsoft.com/en-us/aspnet/core/web-api/index?view=aspnetcore-2.1#annotate-class-with-apicontrollerattribute

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.

  • https://docs.microsoft.com/en-us/dotnet/api/microsoft.aspnetcore.mvc.controllerbase?view=aspnetcore-2.1
like image 32
Riscie Avatar answered Sep 21 '22 03:09

Riscie