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

12,327

Solution 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.

Solution 2

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

Solution 3

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.

Share:
12,327
SeToY
Author by

SeToY

Loves: WPF, SharePoint, LINQ Hates: WPF, SharePoint, LINQ Beauty is more important in computing than anywhere else in technology because software is so complicated. Beauty is the ultimate defence against complexity. - David Gelernter Hilarious. Even as a .NET developer.

Updated on June 26, 2022

Comments

  • SeToY
    SeToY almost 2 years

    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?