【问题标题】:vNext can't see namespace in referenced packagevNext 在引用的包中看不到命名空间
【发布时间】:2014-12-18 00:52:56
【问题描述】:

程序.cs

using System;
using RazorEngine;

namespace ConsoleApp1
{
    public class Program
    {
        public static void Main(string[] args)
        {
            string template = "Hello @Model.Name! Welcome to Razor!";
            string result = Razor.Parse(template, new { Name = "World" });
            Console.WriteLine(result);
            Console.WriteLine("Press enter to exit.");
            Console.ReadLine();
        }
    }
}

项目.json

{
  "version": "1.0.0-*",
  "dependencies": {
    "RazorEngine": "3.4.2"
  },
  "commands": {
    "run": "run"
  },
  "frameworks": {
    "aspnet50": {},
    "aspnetcore50": {
      "dependencies": {
        "System.Console": "4.0.0-beta-*"
      }
    }
  }
}

当我运行 kpm build 时,我得到了这个输出(仅包含相关信息)

Building RazerEngineTester for Asp.Net,Version=v5.0
Using Project dependency RazerEngineTester 1.0.0
Source: /Users/mason/Desktop/Dot Net Solutions/RazerEngineTester/project.json

Using Package dependency RazorEngine 3.4.2
Source: /Users/mason/.kpm/packages/RazorEngine/3.4.2
File: lib/net45/RazorEngine.dll

Building RazerEngineTester for Asp.NetCore,Version=v5.0
Using Project dependency RazerEngineTester 1.0.0
Source: /Users/mason/Desktop/Dot Net Solutions/RazerEngineTester/project.json

Using Package dependency RazorEngine 3.4.2
Source: /Users/mason/.kpm/packages/RazorEngine/3.4.2

Program.cs(2,7): error CS0246: The type or namespace name 'RazorEngine' could not be found (are you missing a using directive or an assembly reference?)

为什么我得到这个编译错误,即使它正确地拉下 RazorEngine NuGet 包,并且according to the source codeRazorEngine 命名空间存在?

【问题讨论】:

  • RazorEngine.Core 是否列在您的参考文献中?如果是这样,它是用波浪形的黄色或红色线条下划线的吗?
  • @JohnKoerner 没有。我所有的参考资料都在上面列出。如果您查看sourceRazor 类不在RazorEngine.Core 命名空间中(尽管它在该文件夹中)。我什至在那个项目中都没有看到 RazorEngine.Core 命名空间。
  • RazorEngine.Core 是 DLL 的名称而不是命名空间的名称。
  • @JohnKoerner 我尝试将RazoreEngine.Core 添加为依赖项,但我得到了Unable to locate RazorEngine.Core >= 3.4.2。如果RazorEngine.CoreRazorEngine 的依赖项,kpm restore 不会自动将其拉入吗?

标签: c# asp.net roslyn asp.net-core


【解决方案1】:

如果你仔细观察 Razor.Parse 方法,它会说下面的事情。

它适用于 asp.net 5.0,但不适用于 core。

现在,如果您从 project.json 中删除框架,那么它将起作用。

   {
    "version": "1.0.0-*",
    "dependencies": {
        "RazorEngine": "3.4.2"
    },
    "commands": { 
        "run" : "run"
    },
    "frameworks" : {
        "aspnet50" : { }        
    }
}

【讨论】:

  • 谢谢,这是正确的。我比你早一点得出这个结论。但是,我没有方便的 Intellisense 来告诉我这一点,因为我目前正在我的 Mac 上进行开发。使用命令行和 Sublime Text。
  • 太棒了。如果它适合你。命令行很棒,它提醒我当我的 RAM 有限时仍然需要编译 C# 程序。我想在 MAC 上探索 vnext,但我没有 :)
  • 使用 OS X 很不错(我用的是将近 7 年的 MacBook),但它确实让您怀念 Visual Studio。嘿,我们在 Mac、iOS 和 Android 上安装了 Office。也许下一个 MS 会决定在 OS X 上做 VS!
【解决方案2】:

我注意到即使我遇到了那个编译错误,如果我运行k run,它也会正常运行。然后我意识到aspnetcore50 框架构建失败,但我并不真正关心那个,而且aspnet50 框架编译得很好。所以我修改了我的 project.json 文件以删除该框架并且它起作用了。

{
  "version": "1.0.0-*",
  "dependencies": {
    "RazorEngine": "3.4.2"
  },
  "commands": {
    "run": "run"
  },
  "frameworks": {
    "aspnet50": {}
  }
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-01-23
    • 1970-01-01
    • 2011-03-21
    • 1970-01-01
    • 2014-10-15
    相关资源
    最近更新 更多