代码之家  ›  专栏  ›  技术社区  ›  user979331

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

  •  0
  • user979331  · 技术社区  · 6 年前

    我试图使用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;
    }
    

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

    我做错了什么?

    0 回复  |  直到 6 年前
        1
  •  0
  •   user7217806    6 年前

    一种选择是将通话转移到 BuildFile 进入控制器的操作方法,其中控制器上下文作为名为 ControllerContext .

    如果需要像示例中那样手动创建控制器,则必须自己创建上下文。 德里克·科马汀(Derek Comartin)在他的博客文章中展示了这一点 Using Razor in a Console Application (outside of ASP.NET Core MVC) 如何为ASP。核心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。扩展。平台摘要包。

    推荐文章