gpt4 book ai didi

c# - 从 AspNetCore TestHost 运行时编译 Razor 期间出现 CompilationFailedException

转载 作者:行者123 更新时间:2023-12-05 02:55:05 54 4
gpt4 key购买 nike

我有 ASP.Net MVC 应用程序,其中一部分是将 razor View 编译为字符串。该代码与此示例非常相似: https://long2know.com/2017/08/rendering-and-emailing-embedded-razor-views-with-net-core/

我是这样在Startup.cs中注册Razor引擎的:

var viewAssembly = typeof(HtmlGeneratorService).GetTypeInfo().Assembly;
var fileProvider = new EmbeddedFileProvider(
viewAssembly,
"ApplicationServices.Widgets.Html.Templates");

services.Configure<MvcRazorRuntimeCompilationOptions>(options => {
options.FileProviders.Clear();
options.FileProviders.Add(fileProvider);
});

services.AddRazorPages().AddRazorRuntimeCompilation();

在测试项目中我有这个设置:

var builder = new HostBuilder()
.ConfigureWebHost(webHost =>
{
webHost.UseTestServer();
webHost.UseStartup<Startup>();
});

var host = await builder.StartAsync();

HttpClient = host.GetTestClient();

但是当我使用这个 HttpClient 调用我的端点时,IRazorViewEngine.GetView 开始抛出奇怪的异常:

Microsoft.AspNetCore.Mvc.Razor.RuntimeCompilation.CompilationFailedException: 'One or more compilation failures occurred:
rnouw0xu.21w(4,41): error CS0234: The type or namespace name 'Razor' does not exist in the namespace 'Microsoft.AspNetCore' (are you missing an assembly reference?)
rnouw0xu.21w(4,82): error CS0518: Predefined type 'System.Type' is not defined or imported
rnouw0xu.21w(4,110): error CS0518: Predefined type 'System.String' is not defined or imported
rnouw0xu.21w(4,127): error CS0518: Predefined type 'System.String' is not defined or imported
rnouw0xu.21w(8,11): error CS0246: The type or namespace name 'System' could not be found (are you missing a using directive or an assembly reference?)
rnouw0xu.21w(9,11): error CS0246: The type or namespace name 'System' could not be found (are you missing a using directive or an assembly reference?)
rnouw0xu.21w(10,11): error CS0246: The type or namespace name 'System' could not be found (are you missing a using directive or an assembly reference?)
rnouw0xu.21w(11,11): error CS0246: The type or namespace name 'System' could not be found (are you missing a using directive or an assembly reference?)
rnouw0xu.21w(13,36): error CS0234: The type or namespace name 'Rendering' does not exist in the namespace 'Microsoft.AspNetCore.Mvc' (are you missing an assembly reference?)
rnouw0xu.21w(14,36): error CS0234: The type or namespace name 'ViewFeatures' does not exist in the namespace 'Microsoft.AspNetCore.Mvc' (are you missing an assembly reference?)
rnouw0xu.21w(29,35): error CS0234: The type or namespace name 'Razor' does not exist in the namespace 'Microsoft.AspNetCore' (are you missing an assembly reference?)
rnouw0xu.21w(29,78): error CS0518: Predefined type 'System.String' is not defined or imported
rnouw0xu.21w(29,87): error CS0518: Predefined type 'System.String' is not defined or imported

我尝试以多种不同的方式修复此错误,但看起来我被困在这里了。

最佳答案

看起来我在这篇文章中找到了答案: https://github.com/aspnet/Razor/issues/1212

我刚刚将此代码添加到我的 test.csproj 文件中:

<Target Name="CopyDepsFiles" AfterTargets="Build" Condition="'$(TargetFramework)'!=''">

<ItemGroup>
<DepsFilePaths Include="$([System.IO.Path]::ChangeExtension('%(_ResolvedProjectReferencePaths.FullPath)', '.deps.json'))" />
</ItemGroup>

<Copy SourceFiles="%(DepsFilePaths.FullPath)" DestinationFolder="$(OutputPath)" Condition="Exists('%(DepsFilePaths.FullPath)')" />

</Target>

关于c# - 从 AspNetCore TestHost 运行时编译 Razor 期间出现 CompilationFailedException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61526883/

54 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com