c# - ASP.NET Core 1.0 RC2 中的 "The type or namespace name could not be found"

标签 c# asp.net asp.net-core-mvc asp.net-core-1.0

我目前正在尝试 ASP.NET Core 1.0 RC2。我将其创建为 .NET Framework 项目(而不是 .NET Core 项目),并使用 .NET Framework 4.5 通过项目引用添加了对我们的 Models 库的引用:

"frameworks": {
  "net46": {
    "dependencies": {
      "Project.Core": {
        "target": "project"
      },
      "Project.DataAccess": {
        "target": "project"
      },
      "Project.Encryption": {
        "target": "project"
      },
      "Project.Models": {
        "target": "project"
      },
      "Project.Resources": {
        "target": "project"
      }
    }
  }
},

现在向我的 View 添加模型指令时,出现以下错误:

@model System.Collections.Generic.List<Project.Models.User>

The type or namespace name 'Project' could not be found (are you missing a using directive or an assembly reference?)
    public class _Views_Home_Index_cshtml : Microsoft.AspNetCore.Mvc.Razor.RazorPage<System.Collections.Generic.List<Project.Models.User>>
The type or namespace name 'Project' could not be found (are you missing a using directive or an assembly reference?)
        public Microsoft.AspNetCore.Mvc.Rendering.IHtmlHelper<System.Collections.Generic.List<Project.Models.User>> Html { get; private set; }

它还在智能感知中显示:无法解析标记“Project.Models.User”无法解析符号“model”

我添加了一个项目引用,添加了一个using语句......仍然出现这个错误。这是为什么?

最佳答案

这是 RC2 中的错误 with an open issue . workaround在对我有用的问题讨论中:

services.AddMvc()
.AddRazorOptions(options =>
{
    var previous = options.CompilationCallback;
    options.CompilationCallback = context =>
    {
        previous?.Invoke(context);
        context.Compilation = context.Compilation.AddReferences(MetadataReference.CreateFromFile(typeof(MyClass).Assembly.Location));
    };
    });

在您的示例中,您需要为 Project.Models.User 执行此操作。

不确定是否4.6.1 and Update 2 is needed for both projects ,我只尝试过。

关于c# - ASP.NET Core 1.0 RC2 中的 "The type or namespace name could not be found",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37589250/

相关文章:

c# - WPF 使用 Magstrip 编码打印到打印机

c# - 如何使 UI 在 Winforms 中响应

c# - EF5 中的连接属性

c# - 使用 Entity Framework 创建数据库索引

asp.net - asp.net 如何去除空格?

jquery - Asp文件上传控件是否有文件

asp.net-core - 如何在MVC核心(1.0.0)中读取xml文档?

asp.net - dot net mvc5 Controller 中的异步函数造成死锁

c# - ASP.NET Core 登录中的 ReturnUrl 为空

c# - Asp.Net 5 TagHelper 模型状态绑定(bind)