'Missing type/namespace in ASP.NET Core MVC

I've created an empty ASP.NET Core Mvc project in visual studio for Mac, added a few dependencies

  • Microsoft.AspNetCore.Mvc (1.1.2)
  • Microsoft.AspNetCore.Mvc.Core (1.1.2)

and also imported the namespaces. However, when I create a new HomeController class and try to inherit from the Controller class

public class HomeController : Controller

I get a red squiggle line and an error:

the type of namespace name 'Controller' could not be found".

Also, I can't seem to find the project.json and csproj files in my project.

enter image description here



Solution 1:[1]

You need to reference Microsoft.AspNetCore as well. This package contains references to a number of other packages that you need to build ASP.NET Core applications.

Try this working csproj file:

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

  <PropertyGroup>
    <TargetFramework>netcoreapp1.1</TargetFramework>
  </PropertyGroup>

  <ItemGroup>
    <PackageReference Include="Microsoft.AspNetCore" Version="1.1.1" />
    <PackageReference Include="Microsoft.AspNetCore.Mvc" Version="1.1.2" />
    <PackageReference Include="Microsoft.AspNetCore.StaticFiles" Version="1.1.1" />
    <PackageReference Include="Microsoft.Extensions.Logging.Debug" Version="1.1.1" />
  </ItemGroup>

</Project>

Also, I can't seem to find the project.json and csproj files in my project.

project.json has been deprecated, so you won't see it in your project.

You do have a .csproj file, though. Visual Studio for Mac is representing it as the LanguageFeatures element in the tree view. You should also be able to see it if you ls in your current directory.

Solution 2:[2]

So I finally solved this by removing and reinstalling Microsoft.AspNetCore.Mvc then I rebuilt the project.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Community
Solution 2 Inaloz_H