加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

c# – “ASP.NET Core 1.0 RC2中找不到”类型或命名空间名称“

发布时间:2020-12-15 04:32:47 所属栏目:百科 来源:网络整理
导读:我目前正在尝试ASP.NET Core 1.0 RC2.我已将其创建为.NET Framework项目(而不是.NET Core项目),并使用.NET Framework 4.5通过项目引用添加了对Models库的引用: "frameworks": { "net46": { "dependencies": { "Project.Core": { "target": "project" },"Pro
我目前正在尝试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 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,我只是尝试过.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读