问题
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-*"
}
}
}
}
When I run kpm build
, I get this output (included only relevant info)
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?)
Why am I getting this compilation error even though it pulls down the RazorEngine NuGet package correctly, and according to the source code, the RazorEngine
namespace exists?
回答1:
if you look at closely to Razor.Parse method, it will say following thing.
It is available for asp.net 5.0 but not for core.

Now if you remove framework from project.json then it will work.
{
"version": "1.0.0-*",
"dependencies": {
"RazorEngine": "3.4.2"
},
"commands": {
"run" : "run"
},
"frameworks" : {
"aspnet50" : { }
}
}
回答2:
I noticed that even though I got that compilation error, if I ran k run
then it would run properly. So then I realized that the aspnetcore50
framework was failing to build, but I didn't really care about that one anyways, and that the aspnet50
framework compiled just fine. So I modified my project.json file to remove that framework and it worked.
{
"version": "1.0.0-*",
"dependencies": {
"RazorEngine": "3.4.2"
},
"commands": {
"run": "run"
},
"frameworks": {
"aspnet50": {}
}
}
来源:https://stackoverflow.com/questions/27537496/vnext-cant-see-namespace-in-referenced-package