簡體   English   中英

如何在Web API控制器中訪問MVC控制器以從視圖獲取pdf

[英]How to access mvc controller in web api controller to get pdf from view

我為單頁Web應用程序創建了Web Api和MVC。 我想調用Web api並渲染mvc控制器以使用Rotativa api從視圖創建pdf。 問題是,當我在Web API中訪問MVC控制器時,它不起作用。

我如何在Web API中訪問MVC控制器以從視圖獲取pdf?

注意:在Web api中聲明的mvc控制器對象,使它在“ GetPdfBytesFormView”方法中給出“ ControllerContext”為空。

網絡API:

[RoutePrefix("api/reports/TestReport")]
public class TestReportController : ApiController
{     
    [HttpPost]
    [Route("GetRequistionPdf")]
    public HttpResponseMessage GetRequistionPdf(modelClass oModel)  
    {       
        HttpResponseMessage response = Request.CreateResponse(HttpStatusCode.OK, "value");
        ReportController _Report = new ReportController();
        response.Content = new ByteArrayContent(_Report.GetPdfBytesFormView(oModel));       
        response.Content.Headers.ContentType = new MediaTypeHeaderValue("application/pdf");            
        return response;
    }
}

MVC控制器:

public class ReportController : Controller
{       
    public ActionResult GenerateReport(modelClass oModel)
    {
        return View(oModel);
    }

    public byte[] GetPdfBytesFormView(modelClass oModel)
    {            
        var actionPDF = new Rotativa.ActionAsPdf("GenerateReport", oModel) 
        {                
            PageSize = Size.A4,
            PageOrientation = Orientation.Portrait,
            PageMargins = { Left = 6, Right = 7 }
        };

        byte[] applicationPDFData = actionPDF.BuildPdf(ControllerContext);            
        return applicationPDFData;
    }
}

AngularJS Web API調用

$http.post('http://localhost:54527/api/reports/TestReport/GetRequistionPdf', { data }, { responseType: 'arraybuffer' })
                .success(function (data) {
                    var file = new Blob([data], { type: 'application/pdf' });
                    var fileURL = URL.createObjectURL(file);
                    window.open(fileURL);
                });

終於得到解決方案。

假設您的控制器名稱是“ PDFController”,操作名稱是“ GetPDF”。 在您的api控制器中編寫以下代碼

    // Add key value    
    RouteData route = new RouteData();
    route.Values.Add("action", "GetPDF"); // ActionName
    route.Values.Add("controller", "PDF"); // Controller Name
    System.Web.Mvc.ControllerContext newContext = new System.Web.Mvc.ControllerContext(new HttpContextWrapper(System.Web.HttpContext.Current), route, controller);
    controller.ControllerContext = newContext;
    controller.GetPDF();

您已完成。 您的pdf應該會生成。

希望對你有幫助

嘗試用下面的一個替換您的GetRequestionPdf:

public HttpResponse GetRequistionPdf(modelClass oModel)  
    {       
        HttpResponse response = HttpContext.Current.Response;
        ReportController _Report = new ReportController();

                response.Clear()
                response.ClearContent()
                response.ClearHeaders()
                response.Buffer = True
                response.ContentType = "application/pdf"
                response.AddHeader("Content-Disposition", "attachment;filename=xyz.pdf")
                response.BinaryWrite(_Report.GetPdfBytesFormView(oModel));
                response.End()


        return response;
    }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM