RSNL
RSNL

Reputation: 213

Controller dotnetCore class not found

I'm using .net core and have added some 4.5 libraries to my project. This works fine. But Visual Studio 2015 can't seem to find the "Controller" (which is a ASP.net core class) class.

My project.json file looks like this:

 {
  "dependencies": {
    "Microsoft.AspNetCore.Mvc": "1.0.1",
    "Microsoft.AspNetCore.Server.IISIntegration": "1.0.0",
    "Microsoft.AspNetCore.Server.Kestrel": "1.0.0",
    "Microsoft.Extensions.Configuration.EnvironmentVariables": "1.0.0",
    "Microsoft.Extensions.Configuration.FileExtensions": "1.0.0",
    "Microsoft.Extensions.Configuration.Json": "1.0.0",
    "Microsoft.Extensions.Logging": "1.0.0",
    "Microsoft.Extensions.Logging.Console": "1.0.0",
    "Microsoft.Extensions.Logging.Debug": "1.0.0",
    "Microsoft.Extensions.Options.ConfigurationExtensions": "1.0.0",
    "Microsoft.AspNetCore.StaticFiles": "1.0.0-rc2-final"
  },

  "tools": {
    "Microsoft.AspNetCore.Server.IISIntegration.Tools": "1.0.0-preview2-final"
  },

  "frameworks": {
    "net452": {
      "dependencies": {
        "ClassLibrary1": {
          "target": "project"
        },
        "ClassLibrary2": {
          "target": "project"
        }
      }
    }
  },

  "buildOptions": {
    "emitEntryPoint": true,
    "preserveCompilationContext": true
  },

  "runtimeOptions": {
    "configProperties": {
      "System.GC.Server": true
    }
  },

  "publishOptions": {
    "include": [
      "wwwroot",
      "Views",
      "Areas/**/Views",
      "appsettings.json",
      "web.config"
    ]
  },

  "scripts": {
    "postpublish": [ "dotnet publish-iis --publish-folder %publish:OutputPath% --framework %publish:FullTargetFramework%" ]
  }
}

When declaring a controller, the "AspNetCore.Mvc" package is not found. And the "Controller" class is not found.

using Microsoft.AspNetCore.Mvc;
namespace WebInterface.Backend
{
    public class AppController : Controller
    {
    }
}

I'm suspecting that there is something wrong with my project.json but can't seem to find the problem. Any suggestions?

Update:

It seems to work, i can build the project but my inteli sense can't find the class, all the includes (system, Mvc) are not found. But when i build it gives no errors. Can also run the project. Any Idea's?

Upvotes: 2

Views: 2409

Answers (3)

surfmuggle
surfmuggle

Reputation: 5942

In my case (ASP.NET Core 3.1) it was that startup.cs did not contain MapControllerRoute()

Adding MapControllerRoute() fixed it:

app.UseEndpoints(endpoints =>
{
    endpoints.MapRazorPages();
    endpoints.MapControllerRoute(
        name: "default",
        pattern: "{controller=Home}/{action=Index}/{id?}");
});

Taken from add a controller to an ASP.NET Core MVC app

Upvotes: 1

Stephen Lee Parker
Stephen Lee Parker

Reputation: 1265

I know this is late, but hopefully it helps someone...

I had a similar issue, my problem was that I named my folder "Controller" instead of "Controllers"

Once I corrected the folder name, everything worked as expected.

Upvotes: 2

petar.kekez
petar.kekez

Reputation: 214

try running this command in cmd in the directory where the project.json file is:

dotnet restore

Upvotes: 1

Related Questions