我有两个控制器,一个是api控制器,另一个是一个视图控制器,我想做的是从该视图控制器中的视图从api控制器生成PDF ...这可能吗,以及如何我会这样做吗?
API控制器-这是我要生成PDF的地方
public byte[] getReportsPDF(string community, string procedure)
{
byte[] pdfBytes = new byte[] { };
System.Web.Mvc.ControllerContext context = new System.Web.Mvc.ControllerContext();
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 }
};
pdfBytes = actionPDF.BuildFile(context);
}
return pdfBytes;
}
视图控制器-这是我要生成的视图。
public ActionResult RedBluePDF(string community, string procedure)
{
return View();
}
答案 0 :(得分:1)
尝试了很长时间后,终于设法解决了这个问题:
var controller = DependencyResolver.Current.GetService<ViewController>();
RouteData route = new RouteData();
route.Values.Add("action", "RedBluePDF");
route.Values.Add("controller", "ApiController");
ControllerContext newContext = new ControllerContext(new HttpContextWrapper(System.Web.HttpContext.Current), route, controller);
controller.ControllerContext = newContext;
controller.RedBluePDF(community, procedure);
我一直在尝试:
var controller = DependencyResolver.Current.GetService<ControllerB>();
controller.ControllerContext = new ControllerContext(this.Request.RequestContext, controller);
但这会给我错误的pdf,因为它将使用您当前的控制器(在本例中为API控制器)来调用ActionAsPdf()