Rotativa.Netcore 在本地工作但在部署后不工作

Rotativa.Netcore works locally but not after deployment

我在 ASP.NET 核心 2.1.1 项目中使用最新的 Rotativa.NetCore 程序集。 NuGet (https://www.nuget.org/packages/Rotativa.AspNetCore v. 1.0.6) 在部署 (win2016) 上不起作用,但在本地 (win10) 上起作用。

部署时的 IIS 给出 404,错误日志 (stdout) 显示如下:

fail: Microsoft.AspNetCore.Diagnostics.ExceptionHandlerMiddleware[1]
      An unhandled exception has occurred while executing the request.
System.Exception
   at Rotativa.AspNetCore.WkhtmlDriver.Convert(String wkhtmlPath, String switches, String html, String wkhtmlExe)
   at Rotativa.AspNetCore.WkhtmltopdfDriver.ConvertHtml(String wkhtmltopdfPath, String switches, String html)
   at Rotativa.AspNetCore.ViewAsPdf.CallTheDriver(ActionContext context)
   at Rotativa.AspNetCore.AsResultBase.BuildFile(ActionContext context)
   at Rotativa.AspNetCore.AsResultBase.ExecuteResultAsync(ActionContext context)
   at Microsoft.AspNetCore.Mvc.Internal.ResourceInvoker.InvokeResultAsync(IActionResult result)
   at Microsoft.AspNetCore.Mvc.Internal.ResourceInvoker.InvokeNextResultFilterAsync[TFilter,TFilterAsync]()
   at Microsoft.AspNetCore.Mvc.Internal.ResourceInvoker.Rethrow(ResultExecutedContext context)
   at Microsoft.AspNetCore.Mvc.Internal.ResourceInvoker.ResultNext[TFilter,TFilterAsync](State& next, Scope& scope, Object& state, Boolean& isCompleted)
   at Microsoft.AspNetCore.Mvc.Internal.ResourceInvoker.InvokeResultFilters()
   at Microsoft.AspNetCore.Mvc.Internal.ResourceInvoker.InvokeNextResourceFilter()
   at Microsoft.AspNetCore.Mvc.Internal.ResourceInvoker.Rethrow(ResourceExecutedContext context)
   at Microsoft.AspNetCore.Mvc.Internal.ResourceInvoker.Next(State& next, Scope& scope, Object& state, Boolean& isCompleted)
   at Microsoft.AspNetCore.Mvc.Internal.ResourceInvoker.InvokeFilterPipelineAsync()
   at Microsoft.AspNetCore.Mvc.Internal.ResourceInvoker.InvokeAsync()
   at Microsoft.AspNetCore.Builder.RouterMiddleware.Invoke(HttpContext httpContext)
   at Microsoft.AspNetCore.Authentication.AuthenticationMiddleware.Invoke(HttpContext context)
   at Microsoft.AspNetCore.StaticFiles.StaticFileMiddleware.Invoke(HttpContext context)
   at Microsoft.AspNetCore.Diagnostics.ExceptionHandlerMiddleware.Invoke(HttpContext context)

这里是'myproject'来源

public IActionResult DownloadCertificate(int id)
{
    var model = getData(id);
    return new ViewAsPdf("PdfCertificate", model)
    {
        WkhtmlPath = "", // set the path to the Rotativa .exe files. works locally but not on deployment. 
        FileName = "Cert.pdf"
    };
}

我试过将 WkhtmlPath 更改为 "..\Rotativa\" 但没有成功。

疑难解答我发现了这个: - 显然 Rotativa 是针对 .NET Core 1.0 编译的:

There's code to update Rotativa.NetCore from .Net Core 1 to .Net Core 2.0 here: https://github.com/aaxelm/Rotativa.NetCore/pull/1/files?diff=split

所以,我下载了 Rotativa 源并相应地更新到 .NET core 2.0,引用了新程序集。问题仍然存在。

我也检查了权限。 Rotativa 文件夹授予 IIS_IUSRS 执行权限(运行 ApplicationPoolIdentity)。

我错过了什么?

我的本地文件夹结构:

c:\inetpub\myproject\myproject.dll + web.config and what not. c:\inetpub\myproject\Rotativa{wkhtmltoimage.exe, wkhtmltopdf.exe, wkhtmltox.dll}

我部署的文件夹结构:

c:\inetpub\sites\myproject\myproject.dll + web.config and what not. c:\inetpub\sites\myproject\wwwroot\ (static files) c:\inetpub\sites\myproject\Rotativa{wkhtmltoimage.exe, wkhtmltopdf.exe, wkhtmltox.dll}

原来我需要安装 Microsoft Visual C++ Redistributable 2017 (x86) - 在这里下载:https://visualstudio.microsoft.com/vs/older-downloads/ or https://visualstudio.microsoft.com/downloads/

  1. 我恢复到官方 nuget(为了以后更容易维护)https://www.nuget.org/packages/Rotativa.AspNetCore v. 1.0.6
  2. 我将 RotativaConfiguration.Setup(env, "..\Rotativa\"); 添加到 Startup.cs
  3. 我的最终代码如下所示:

代码:

public IActionResult DownloadCertificate(int id)
{
    var model = getData(id);
    return new ViewAsPdf("PdfCertificate", model)
    {
         FileName = "Cert.pdf"
    };
}

我用 asp netcore3.1 开发的项目遇到了同样的问题,并在安装“Microsoft Visual C++ Redistributable 2017 (x86)”后解决了。请确保安装 (x86),无论您的 OS 是 x64 还是 x86!谢谢沙!!

对我来说,我只需要在服务器上安装 Microsoft Visual C++ Redistributable 2017 (x86)。之前我试过其他解决方案,包括在本地安装它们,都没有用,我几乎要放弃了!