找不到控制器dotnetCore类

时间:2017-04-28 13:13:40

标签: c# asp.net .net visual-studio asp.net-core

我正在使用.net核心,并为我的项目添加了一些4.5库。这很好用。但Visual Studio 2015似乎无法找到“Controller”(这是一个ASP.net核心类)类。

我的project.json文件如下所示:

 {
  "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%" ]
  }
}

声明控制器时,找不到“AspNetCore.Mvc”软件包。并且找不到“Controller”类。

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

我怀疑我的project.json有问题,但似乎无法找到问题。有什么建议吗?

更新

它似乎工作,我可以建立项目,但我的inteli意识找不到类,所有包含(系统,Mvc)都找不到。但是当我构建它时没有错误。也可以运行该项目。任何想法?

3 个答案:

答案 0 :(得分:1)

尝试在project.json文件所在的目录中的cmd中运行此命令:

dotnet restore

答案 1 :(得分:0)

我知道这很晚了,但是希望可以对某人有所帮助...

我遇到了类似的问题,我的问题是我将文件夹命名为“ Controller”而不是“ Controllers”

更正文件夹名称后,一切都会按预期进行。

答案 2 :(得分:0)

就我而言(ASP.NET Core 3.1),startup.cs 不包含 MapControllerRoute()

添加 MapControllerRoute() 修复了它:

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

取自add a controller to an ASP.NET Core MVC app