CompilationFailedException when using IRazorViewEngine in non Sdk.Web projects to render Razor pages
我试图在控制台应用程序
1 | _viewEngine.GetView(directory, name, true); |
例外:
Microsoft.AspNetCore.Mvc.Razor.Compilation.CompilationFailedException: One or more compilation failures occurred:
kmcajniq.bah(4,20): error CS0400: The type or namespace name 'Microsoft' could not be found in the global namespace (are you missing an assembly reference?)
kmcajniq.bah(5,19): error CS0400: The type or namespace name 'Microsoft' could not be found in the global namespace (are you missing an assembly reference?)
kmcajniq.bah(4,82): error CS0518: Predefined type 'System.Type' is not defined or imported
kmcajniq.bah(4,115): error CS0518: Predefined type 'System.String' is not defined or imported
kmcajniq.bah(4,132): error CS0518: Predefined type 'System.String' is not defined or imported
kmcajniq.bah(5,81): error CS0518: Predefined type 'System.String' is not defined or imported
以及其他缺少的
但是,当我将项目sdk更改为
什么是
问题在于,
要解决此问题,需要将以下属性添加到
1 2 3 | <PropertyGroup> <PreserveCompilationContext>true</PreserveCompilationContext> </PropertyGroup> |
有关差异的其他信息,可以在这里找到。Microsoft.NET.Sdk和Microsoft.NET.Sdk.Web
之间有什么区别?