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

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

问题描述

我正在尝试在控制台应用程序< Project Sdk = Microsoft.NET.Sdk> IRazorViewEngine $ c>(不是< Project Sdk = Microsoft.NET.Sdk.Web> )来呈现 .cshtml 页面在内存中。我在 ServiceProvider 中注册了每个需要的依赖项。调用以下行时,我得到一个 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' br>
kmcajniq.bah(5,81):错误CS0518:未定义或导入预定义类型'System.String'


和其他丢失的系统 Microsoft 类型。



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

什么是 Microsoft.NET.Sdk 缺少什么 Microsoft.NET.Sdk.Web 可以使用Razor渲染吗?

解决方案

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



要解决该问题,需要在 .csproj 文件中添加以下属性,并将其设置为 true

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

有关差异的其他信息可以在这里找到 Microsoft.NET.Sdk和Microsoft.NET.Sdk.Web之间有什么区别?


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); 

Exception:

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

and other more missing System and Microsoft types.

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

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

解决方案

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.

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

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

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天全站免登陆