1

从一个控制器动作中,我需要调用另一个动作,并且有任何适用ActionFilter于被调用动作的触发。我需要这样做,因为我将生成的 HTML 传递给 PDF 生成器。

这是我目前拥有的,但它没有调用SomeActionFilter.OnActionExecuting()

[SomeActionFilter]
public class FooController : Controller
{
     public ActionResult ActionOne()
     {
          return View();
     }

     public ActionResult ActionTwo()
     {
         // This is how I'm rendering the view to HTML at the moment
         var controllerDescriptor = new ReflectedControllerDescriptor(ControllerContext.Controller.GetType());
         var actionDescriptor = controllerDescriptor.FindAction(ControllerContext, "ActionOne");
         var result = actionDescriptor.Execute(ControllerContext, new Dictionary<string, object>()) as ViewResult;

         ViewEngineResult viewEngineResult = ViewEngines.Engines.FindView(controllerContext, viewName, null);

         var builder = new StringBuilder();
         using (var writer = new StringWriter(builder))
         {
             var context = new ViewContext(controllerContext, viewEngineResult.View, result.ViewData, result.TempData, writer);
             viewEngineResult.View.Render(context, writer);
         }

         string actionOneAsHtml = builder.ToString();
         // From here on I'm just using the rendered HTML to generate the PDF ... 

         byte[] pdf = _acrobatExportService.ExportFromUrl(new AcrobatExportModel
         {
             Html = actionOneAsHtml 
         });

         return File(pdf, "application/pdf", "export.pdf");
     }
}

我知道我可以使用 HTTP 调用该操作,但这似乎是多余的。

4

0 回答 0