私はついにこれを行う方法を得ました。
実際、rotativaのメソッド "return new ViewAsPdf(model)"はHttpResponseStreamを返します。私たちはほとんど何もできません。しかし、アクションがカスタム属性を使用して実行されると、レスポンスを変更/変更できます。アクションフィルタのOnResultExecuted()メソッドをオーバーライドできます。
コントローラのアクション
[HttpGet]
[ActionDownload] //here a custom action filter added
public ActionResult DownloadDocument()
{
var htmlContent = "<h1>sachin Kumar</hi>";
var model = new PdfInfo {FtContent = htmlContent, FtName = "Populate Form"};
return new ViewAsPdf(model);
}
カスタムアクションフィルタ:
public class ActionDownloadAttribute : ActionFilterAttribute
{
public override void OnResultExecuted(ResultExecutedContext filterContext)
{
//Add content-disposition header to response so that browser understand to download as an attachment.
filterContext.HttpContext.Response.AddHeader("content-disposition", "attachment; filename=" + "Report.pdf");
base.OnResultExecuted(filterContext);
}
}
これはなぜ受け入れられた答えではありませんか?シンプルで、余分なコードをほとんど必要とせず、うまく動作します。 – Simon