Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ApiController not found in .NET Core WebApi project

I have created a new .NET Core WebAPI project in Visual Studio Code. All dependencies have been restored. The project is using the following SDK and runtimes:

  • .NET Core SDK 2.1.302
  • Microsoft.AspNetCore.All 2.1.2
  • Microsoft.AspNetCore.App 2.1.2
  • Microsoft.NETCore.App 2.1.2

My controller class (including a screenshot for the Intellisense view):

using Microsoft.AspNetCore.Mvc;
using System.Collections.Generic;
using System.Linq;

namespace Gmn.Controllers
{
    [Route("api/[controller]")]
    [ApiController]
    public class MyController : ControllerBase
    {
        public MyController() {

        }
    }
}

Syntax highlighting shows ApiController not found

Build error:

error CS0246: The type or namespace name 'ApiControllerAttribute' could not be found (are you missing a using directive or an assembly reference?)

The Route attribute, which is also in the Microsoft.AspNetCore.Mvc assembly, resolves correctly, no error. So, I know the assembly is present.

Per MS Docs, the attribute is defined in this assembly:

MS Docs for ApiControllerAttribute

After 3 hours of searching I have not been able to figure this out. I'm new to .NET Core, but not .NET.

Any suggestions how to resolve this issue? Thanks.

like image 348
realmikep Avatar asked Jul 25 '18 14:07

realmikep


People also ask

What is ApiController attribute in .NET core?

The [ApiController] attribute applies inference rules for the default data sources of action parameters. These rules save you from having to identify binding sources manually by applying attributes to the action parameters.

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 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.


1 Answers

I finally figured this out. The .csproj file was not pointing to the correct versions for TargetFramework and PackageReference. They were pointing to 2.0 versions. 2.0 is not even installed, but there were no errors regarding that. (Coming from Visual Studio, I'm used to all that magic being done for me!)

<Project Sdk="Microsoft.NET.Sdk.Web">

  <PropertyGroup>
    <TargetFramework>netcoreapp2.1</TargetFramework>
  </PropertyGroup>

...

  <ItemGroup>
    <PackageReference Include="Microsoft.AspNetCore.All" Version="2.1.2" />
  </ItemGroup>

...

</Project>
like image 82
realmikep Avatar answered Sep 19 '22 17:09

realmikep