迁移到ASP Core 3.1后,控制台中的Razor MVC无法正常工作

问题描述

我们正在从Asp Core 2.1迁移到Asp Core 3.1,一切正常,但是用于测试“从剃刀到字符串”的愚蠢命令行工具不起作用。此命令行工具将我们用于测试目的的剃刀库广告的某些视图保存到文件中。

视图引擎渲染无法找到视图:

system.invalidOperationException: 
'Unable to find view 'Pages/Emails/TicketNewMessage/TicketNewMessageHtml.cshtml'. 
The following locations were searched:/Pages/Emails/TicketNewMessage/TicketNewMessageHtml.cshtml'

位置正确,并且在ASP Core 2.1上可以正常运行,当我们从ASP core 3.1应用程序调用库时,它仍在工作。

与我们联系,问题可能出在渲染的设置上:

        var services = new ServiceCollection();
        var applicationEnvironment = PlatformServices.Default.Application;
        services.AddSingleton(applicationEnvironment);

        var appDirectory = Directory.GetCurrentDirectory();

        var environment = new HostingEnvironment
        {
            ApplicationName = Assembly.GetEntryAssembly().GetName().Name,ContentRootFileProvider = new PhysicalFileProvider(appDirectory),};
        services.AddSingleton<IHostEnvironment>(environment);

        services.Configure<MvcRazorRuntimeCompilationoptions>(options =>
        {
            options.FileProviders.Clear();
            options.FileProviders.Add(new PhysicalFileProvider(appDirectory));
        });


        services.AddSingleton<ObjectPoolProvider,DefaultObjectPoolProvider>();

        var diagnosticSource = new DiagnosticListener("Microsoft.AspNetCore");
        services.AddSingleton<DiagnosticSource>(diagnosticSource);
        services.AddSingleton(diagnosticSource);

        services.AddRazorPages();
        services.AddLogging();
        services.AddSingleton<RazorViewToStringRenderer>();
        var provider = services.BuildServiceProvider();
        return provider.GetrequiredService<RazorViewToStringRenderer>();

缺少什么?

最诚挚的问候

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)