“The type or namespace name could not be found” in ASP.NET Core 1.0 RC2

社会主义新天地 提交于 2019-12-23 09:42:04

问题


I'm currently trying ASP.NET Core 1.0 RC2. I've created it as a .NET Framework project (as opposed to a .NET Core project) and added references to our Models library, using .NET Framework 4.5, via a project reference:

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

Now when adding a model directive to my view, the following error occurs:

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

It also displays in intellisense: Cannot resolve tag 'Project.Models.User' and Cannot resolve symbol 'model'

I have added a project reference, added a using statement... Still this error occurs. Why is that?


回答1:


This is a bug in RC2 with an open issue. A workaround in the issue discussion that works for me is:

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

In your example, you'd need to do this for Project.Models.User.

Not sure whether 4.6.1 and Update 2 is needed for both projects, I've only tried with that.




回答2:


The class library project has to have been created in Visual Studio 2015 Update 2 and it must use .NET Framework 4.6.1. And your ASP.NET Core project must use .NET Framework 4.6.1 as well.

RC2 is the first version that supposedly supports including class libraries. But I've found that if your class library has certain dependencies (like System.DirectoryServices.AccountManagement) it will fail to load at runtime.




回答3:


I fixed it by examining the file _ViewImports.cshtml. That is where all usings go that get loaded in to all views.

For Instance -

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



回答4:


Ensuring that the preserveCompilationContext is present and true in the project.json buildOptions has fixed this issue for me with Visual Studio Code on Ubuntu

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


来源:https://stackoverflow.com/questions/37589250/the-type-or-namespace-name-could-not-be-found-in-asp-net-core-1-0-rc2

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!