Rotativa值不能为空。 参数名:上下文(Rotativa Value cannot be

2019-10-28 12:26发布

我试图用Rotativa生成PDF并返回字节,但我得到的错误:

值不能为空。 参数名:上下文

这里是我的代码:

public string getReportsPDF(string community, string procedure)
{
    SiteSuperReportsController controller = new SiteSuperReportsController();

    string value = "";
    byte[] pdfBytes = new byte[] { };

    if (procedure == "GetProductionTasks")
    {
        var actionPDF = new Rotativa.ActionAsPdf("RedBluePDF", new { community = community, procedure = procedure })
        {
            PageSize = Size.A4,
            PageOrientation = Rotativa.Options.Orientation.Landscape,
            PageMargins = { Left = 1, Right = 1 }
        };
        try
        {
            pdfBytes = actionPDF.BuildFile(controller.ControllerContext);
            value = "Works";
        }
        catch(Exception e)
        {
            value = e.Message.ToString();
        }
    }
    return value;
}

返回的值是值不能为空。 参数名:上下文

我究竟做错了什么?

Answer 1:

一种选择是将呼叫移动BuildFile到控制器,其中控制器方面可作为财产所谓的操作方法ControllerContext

如果你需要在你的榜样手动创建你的控制器一样,你必须自己创造的环境。 德里克Comartin显示在他的博客在控制台应用程序(ASP.NET MVC核心外)用剃刀如何为ASP.Core 2项目做到这一点。 对于您的情况,请尝试更改

pdfBytes = actionPDF.BuildFile(controller.ControllerContext);

pdfBytes = actionPDF.BuildFile(CreateDummyControllerContext("SiteSuperReports"));

使用以下方法:

    private ControllerContext CreateDummyControllerContext(string controllerName)
    {
        var context = new ControllerContext
        {
            HttpContext = new DefaultHttpContext
            {
                RequestServices = GetServiceProvider()
            },
            RouteData = new RouteData
            {
                Values = {{"controller", controllerName}}
            },
            ActionDescriptor = new Microsoft.AspNetCore.Mvc.Controllers.ControllerActionDescriptor
            {
                RouteValues = new Dictionary<string, string>(),
            }
        };

        return context;
    }

    // see https://codeopinion.com/using-razor-in-a-console-application/
    private ServiceProvider GetServiceProvider()
    {
        var services = new ServiceCollection();
        services.AddSingleton(PlatformServices.Default.Application);
        var environment = new HostingEnvironment
        {
            ApplicationName = Assembly.GetEntryAssembly().GetName().Name
        };
        services.AddSingleton<IHostingEnvironment>(environment);
        services.Configure<RazorViewEngineOptions>(options =>
        {
            options.FileProviders.Clear();
            options.FileProviders.Add(new PhysicalFileProvider(Directory.GetCurrentDirectory()));
        });
        services.AddSingleton<ObjectPoolProvider, DefaultObjectPoolProvider>();
        services.AddSingleton<DiagnosticSource>(new DiagnosticListener("Microsoft.AspNetCore"));

        services.AddLogging();
        services.AddMvc();

        return services.BuildServiceProvider();
    }

您可能需要添加Microsoft.Extensions.PlatformAbstractions包。



文章来源: Rotativa Value cannot be null. Parameter name: context