realmikep
realmikep

Reputation: 673

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:

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.

Upvotes: 10

Views: 19408

Answers (2)

realmikep
realmikep

Reputation: 673

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>

Upvotes: 7

Shahriat Hossain
Shahriat Hossain

Reputation: 340

Have you added this one : services.AddMvc() .SetCompatibilityVersion(CompatibilityVersion.Version_2_1);

In your startup class ?

Upvotes: -2

Related Questions