[英]“The type or namespace name could not be found” in ASP.NET Core 1.0 RC2
我目前正在嘗試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"
}
}
}
},
現在,在我的視圖中添加模型指令時,會發生以下錯誤:
@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; }
它還顯示在intellisense中: 無法解析標簽'Project.Models.User'並且無法解析符號'model'
我添加了一個項目引用,添加了一個using語句......仍然會發生此錯誤。 這是為什么?
這是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));
};
});
在您的示例中,您需要為Project.Models.User
執行此操作。
不確定兩個項目是否需要4.6.1和更新2 ,我只嘗試過。
必須在Visual Studio 2015 Update 2中創建類庫項目,並且必須使用.NET Framework 4.6.1。 您的ASP.NET Core項目也必須使用.NET Framework 4.6.1。
RC2是第一個支持包括類庫的版本。 但我發現如果你的類庫有某些依賴項(如System.DirectoryServices.AccountManagement
),它將無法在運行時加載。
我通過檢查文件_ViewImports.cshtml來修復它。 這就是所有使用的內容都被加載到所有視圖中。
例如 -
@using MyProject
@using MyProject.Models
@using MyProject.Models.AccountViewModels
@using MyProject.Models.ManageViewModels
@using Microsoft.AspNetCore.Identity
@addTagHelper *, Microsoft.AspNetCore.Mvc.TagHelpers
在project.json buildOptions中確保preserveCompilationContext存在且為true已在Ubuntu上使用Visual Studio Code修復了此問題
{
"buildOptions": {
"emitEntryPoint": true,
"warningsAsErrors": true,
"preserveCompilationContext": true
},
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.