我目前正在尝试 ASP.NET Core 1.0 RC2。我将其创建为 .NET Framework 项目(而不是 .NET Core 项目),并使用 .NET Framework 4.5 通过项目引用添加了对我们的 Models
库的引用:
"frameworks": {
"net46": {
"dependencies": {
"Project.Core": {
"target": "project"
},
"Project.DataAccess": {
"target": "project"
},
"Project.Encryption": {
"target": "project"
},
"Project.Models": {
"target": "project"
},
"Project.Resources": {
"target": "project"
}
}
}
},
现在向我的 View 添加模型指令时,出现以下错误:
@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; }
它还在智能感知中显示:无法解析标记“Project.Models.User” 和无法解析符号“model”
我添加了一个项目引用,添加了一个using语句......仍然出现这个错误。这是为什么?
最佳答案
这是 RC2 中的错误 with an open issue . workaround在对我有用的问题讨论中:
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));
};
});
在您的示例中,您需要为 Project.Models.User
执行此操作。
不确定是否4.6.1 and Update 2 is needed for both projects ,我只尝试过。
关于c# - ASP.NET Core 1.0 RC2 中的 "The type or namespace name could not be found",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37589250/