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() {
}
}
}
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:
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.
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.
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.
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.
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>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With