在非 Sdk.Web 项目中使用 IrazorViewEngine 渲染 Razor 页面时出现 CompilationFailedException [英] CompilationFailedException when using IRazorViewEngine in non Sdk.Web projects to render Razor pages

查看:17
本文介绍了在非 Sdk.Web 项目中使用 IrazorViewEngine 渲染 Razor 页面时出现 CompilationFailedException的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在控制台应用程序中使用 IRazorViewEngine <Project Sdk="Microsoft.NET.Sdk">(不是 <Project Sdk="Microsoft.NET.Sdk.Web">) 在内存中呈现 .cshtml 页面.我在 ServiceProvider 中注册了所有需要的依赖项.在调用以下行时,我得到一个 CompilationFailedException:

I am trying to use IRazorViewEngine in a console application <Project Sdk="Microsoft.NET.Sdk"> (not <Project Sdk="Microsoft.NET.Sdk.Web">) to render a .cshtml page in memory. I registered every needed dependency in the ServiceProvider. While calling the following line I get a CompilationFailedException:

_viewEngine.GetView(directory, name, true); 

异常:

Microsoft.AspNetCore.Mvc.Razor.Compilation.CompilationFailedException:发生一个或多个编译失败:
kmcajniq.bah(4,20):错误 CS0400:在全局命名空间中找不到类型或命名空间名称Microsoft"(您是否缺少程序集引用?)
kmcajniq.bah(5,19):错误 CS0400:在全局命名空间中找不到类型或命名空间名称Microsoft"(您是否缺少程序集引用?)
kmcajniq.bah(4,82):错误 CS0518:未定义或导入预定义类型System.Type"
kmcajniq.bah(4,115):错误 CS0518:未定义或导入预定义类型System.String"
kmcajniq.bah(4,132):错误 CS0518:未定义或导入预定义类型System.String"
kmcajniq.bah(5,81):错误 CS0518:未定义或导入预定义类型System.String"

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

以及其他更多缺失的 SystemMicrosoft 类型.

and other more missing System and Microsoft types.

但是,当我将项目 sdk 更改为 Microsoft.NET.Sdk.Web 时,一切正常.

However, when I'm changing the project sdk to Microsoft.NET.Sdk.Web everything works fine.

什么是 Microsoft.NET.Sdk 缺少 Microsoft.NET.Sdk.Web 所做的,Razor 渲染工作?

What is Microsoft.NET.Sdk missing what Microsoft.NET.Sdk.Web does, that Razor rendering works?

推荐答案

问题是,RazorViewEngine 在运行时需要编译上下文来编译 Razor 页面.默认设置在 Microsoft.NET.Sdk 中设置为 false,而在 Microsoft.NET.Sdk.Web 中设置为 true.

The problem was, that the RazorViewEngine needs the compilation context to compile the Razor pages during run time. The default setting for this is in Microsoft.NET.Sdk set to false, while it is set in Microsoft.NET.Sdk.Web to true.

要解决此问题,需要将以下属性添加到 .csproj 文件并设置为 true:

To fix the problem the following property needs to be added to the .csproj file and set to true:

<PropertyGroup>
    <PreserveCompilationContext>true</PreserveCompilationContext>
</PropertyGroup>

可以在此处找到有关差异的其他信息 Microsoft.NET.Sdk 和 Microsoft.NET.Sdk.Web 有什么区别

Additional info about differences can be found here What are the differences between Microsoft.NET.Sdk and Microsoft.NET.Sdk.Web

这篇关于在非 Sdk.Web 项目中使用 IrazorViewEngine 渲染 Razor 页面时出现 CompilationFailedException的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆