在控制器操作中,我需要调用另一个操作,并且在被调用的操作上也有任何适用的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调用该操作,但这似乎是多余的。