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:
我目前正在尝试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"
}
}
}
},
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'
它还显示在intellisense中:无法解析标签'Project.Models.User'并且无法解析符号'model'
I have added a project reference, added a using statement... Still this error occurs. Why is that?
我添加了一个项目引用,添加了一个using语句......仍然会发生此错误。这是为什么?
4 个解决方案
#1
7
This is a bug in RC2 with an open issue. A workaround in the issue discussion that works for me is:
这是RC2中存在未解决问题的错误。问题讨论中的解决方法对我有用:
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
.
在您的示例中,您需要为Project.Models.User执行此操作。
Not sure whether 4.6.1 and Update 2 is needed for both projects, I've only tried with that.
不确定两个项目是否需要4.6.1和更新2,我只尝试过。
#2
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.
必须在Visual Studio 2015 Update 2中创建类库项目,并且必须使用.NET Framework 4.6.1。您的ASP.NET Core项目也必须使用.NET Framework 4.6.1。
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.
RC2是第一个支持包括类库的版本。但是我发现如果你的类库有某些依赖项(比如System.DirectoryServices.AccountManagement),它将无法在运行时加载。
#3
1
I fixed it by examining the file _ViewImports.cshtml. That is where all usings go that get loaded in to all views.
我通过检查文件_ViewImports.cshtml来修复它。这就是加载到所有视图的所有使用的地方。
For Instance -
例如 -
@using MyProject
@using MyProject.Models
@using MyProject.Models.AccountViewModels
@using MyProject.Models.ManageViewModels
@using Microsoft.AspNetCore.Identity
@addTagHelper *, Microsoft.AspNetCore.Mvc.TagHelpers
#4
0
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
确保在project.json中使用preserveCompilationContext并且为true。在Ubuntu上使用Visual Studio Code为我修复了这个问题
{
"buildOptions": {
"emitEntryPoint": true,
"warningsAsErrors": true,
"preserveCompilationContext": true
},
#1
7
This is a bug in RC2 with an open issue. A workaround in the issue discussion that works for me is:
这是RC2中存在未解决问题的错误。问题讨论中的解决方法对我有用:
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
.
在您的示例中,您需要为Project.Models.User执行此操作。
Not sure whether 4.6.1 and Update 2 is needed for both projects, I've only tried with that.
不确定两个项目是否需要4.6.1和更新2,我只尝试过。
#2
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.
必须在Visual Studio 2015 Update 2中创建类库项目,并且必须使用.NET Framework 4.6.1。您的ASP.NET Core项目也必须使用.NET Framework 4.6.1。
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.
RC2是第一个支持包括类库的版本。但是我发现如果你的类库有某些依赖项(比如System.DirectoryServices.AccountManagement),它将无法在运行时加载。
#3
1
I fixed it by examining the file _ViewImports.cshtml. That is where all usings go that get loaded in to all views.
我通过检查文件_ViewImports.cshtml来修复它。这就是加载到所有视图的所有使用的地方。
For Instance -
例如 -
@using MyProject
@using MyProject.Models
@using MyProject.Models.AccountViewModels
@using MyProject.Models.ManageViewModels
@using Microsoft.AspNetCore.Identity
@addTagHelper *, Microsoft.AspNetCore.Mvc.TagHelpers
#4
0
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
确保在project.json中使用preserveCompilationContext并且为true。在Ubuntu上使用Visual Studio Code为我修复了这个问题
{
"buildOptions": {
"emitEntryPoint": true,
"warningsAsErrors": true,
"preserveCompilationContext": true
},