Program.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();
}
}
}
project.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 code,RazorEngine
命名空间存在?
答案 0 :(得分:2)
如果仔细观察Razor.Parse方法,它会说下面的事情。
它适用于asp.net 5.0但不适用于核心。
现在,如果从project.json中删除框架,那么它将起作用。
{
"version": "1.0.0-*",
"dependencies": {
"RazorEngine": "3.4.2"
},
"commands": {
"run" : "run"
},
"frameworks" : {
"aspnet50" : { }
}
}
答案 1 :(得分:0)
我注意到即使我收到了编译错误,如果我运行k run
那么它也会运行正常。所以我意识到aspnetcore50
框架无法构建,但我还是不关心那个,并且aspnet50
框架编译得很好。所以我修改了我的project.json文件以删除该框架并且它有效。
{
"version": "1.0.0-*",
"dependencies": {
"RazorEngine": "3.4.2"
},
"commands": {
"run": "run"
},
"frameworks": {
"aspnet50": {}
}
}