简体   繁体   中英

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

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?

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.

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.

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

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
    },

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM