Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

"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?

like image 233
SeToY Avatar asked Jun 02 '16 10:06

SeToY


1 Answers

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.

like image 60
bzlm Avatar answered Nov 14 '22 21:11

bzlm