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

I am currently trying to use ASP.NET Core 1.0 RC2. I created it as a .NET Framework project (as opposed to a .NET Core project) and added links to our Models library using the .NET Framework 4.5 using a project link:

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

Now, adding the 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 is also displayed in intellisense: cannot resolve the tag 'Project.Models.User' and cannot resolve the symbol 'model'

I added a link to the project, added a using statement ... This error occurs. Why is this?

+6
source share
4 answers

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

 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 need to do this for Project.Models.User .

I'm not sure that 4.6.1 and update 2 are necessary for both projects , I only tried with this.

+7
source

The class library project must be created in Visual Studio 2015 Update 2, and it must use the .NET Framework 4.6.1. And your ASP.NET Core project should also use the .NET Framework 4.6.1.

RC2 is the first version that supposedly supports the inclusion of class libraries. But I found that if your class library has certain dependencies (for example, System.DirectoryServices.AccountManagement ), it will not load at runtime.

+3
source

I fixed it by viewing the _ViewImports.cshtml file. That's where all the games go that load in all kinds.

For instance -

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

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

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

All Articles