【问题标题】:"The type or namespace name could not be found" in ASP.NET Core 1.0 RC2ASP.NET Core 1.0 RC2 中的“找不到类型或命名空间名称”
【发布时间】:2016-06-02 10:27:54
【问题描述】:

我目前正在尝试 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"
      }
    }
  }
},

现在在我的视图中添加模型指令时,会出现以下错误:

@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; }

它还在智能感知中显示:Cannot resolve tag 'Project.Models.User'Cannot resolve symbol 'model'

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

【问题讨论】:

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


    【解决方案1】:

    这是 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,我只试过了。

    【讨论】:

    • Visual Studio 2017 下的 ASP.NET Core 1.1 中仍然存在此错误。
    • github.com/aspnet/Mvc/issues/4686github.com/aspnet/Razor/issues/755 的cmets 看来,人们已经成功解决了这个问题。如果您尝试那里描述的方法会怎样? @MichaelMortensen
    • 我用你的回答解决了这个问题,我向微软提交了一个错误。我正在运行 VS2017。
    【解决方案2】:

    类库项目必须已在 Visual Studio 2015 Update 2 中创建,并且必须使用 .NET Framework 4.6.1。而且您的 ASP.NET Core 项目也必须使用 .NET Framework 4.6.1。

    RC2 是第一个据称支持包括类库的版本。但是我发现如果你的类库有某些依赖(比如System.DirectoryServices.AccountManagement),它会在运行时加载失败。

    【讨论】:

    • 使用 .NET Framework 4.6.1 创建库项目(由 VS 2010 或几年前创建)时是否有真正的区别?
    • 是的,差别很大。这是微软开发人员显然正在努力解决的问题。我已经确认它仅在两个项目都在 VS 2015 Update 2 中创建并且都使用 4.6.1 时才有效。请记住,这是预发布版本,情况可能会发生变化。但就目前而言,就是这样。
    • Clint - 我很高兴你选择继续贡献。我赞成这个答案。
    • 我现在过来检查一下。我已将项目更改为在 .NET FX 4.6.1 上构建,项目文件的元数据看起来与使用 VS 2015 Update 2 创建的完全相同。但是,错误仍然存​​在。 (注:仍然是类库,不是 .NET Core 类库)
    • 更新:我还创建了一个新的 ASP.NET Core Framework 项目和一个新的类库,但仍然无济于事。不工作。 The type or namespace name 'ClassLibrary1' could not be found (are you missing a using directive or an assembly reference?) public class _Views_Home_About_cshtml : Microsoft.AspNetCore.Mvc.Razor.RazorPage&lt;IEnumerable&lt;ClassLibrary1.Class1&gt;&gt;
    【解决方案3】:

    我通过检查文件 _ViewImports.cshtml 修复了它。这就是所有使用加载到所有视图的地方。

    例如-

    @using MyProject
    @using MyProject.Models
    @using MyProject.Models.AccountViewModels
    @using MyProject.Models.ManageViewModels
    @using Microsoft.AspNetCore.Identity
    @addTagHelper *, Microsoft.AspNetCore.Mvc.TagHelpers
    

    【讨论】:

      【解决方案4】:

      确保在 project.json buildOptions 中存在 preserveCompilationContext 并且为 true,从而在 Ubuntu 上使用 Visual Studio Code 为我解决了这个问题

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

      【讨论】:

        猜你喜欢
        • 2013-03-25
        • 1970-01-01
        • 1970-01-01
        • 2016-06-27
        • 2011-02-24
        • 1970-01-01
        • 2020-02-03
        • 2017-07-12
        • 2011-05-13
        相关资源
        最近更新 更多