如何在Web API控制器中访问MVC控制器以从视图获取pdf [英] How to access mvc controller in web api controller to get pdf from view

查看:95
本文介绍了如何在Web API控制器中访问MVC控制器以从视图获取pdf的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我为单页Web应用程序创建了Web Api和MVC.我想调用Web api并渲染mvc控制器以使用Rotativa api从视图创建pdf.问题是,当我在Web API中访问MVC控制器时,它不起作用.

I created Web Api and MVC combined for single page web app. I want to call web api and render mvc controller to create pdf from view using Rotativa api. Problem is when i access mvc controller in web api it's not work.

我如何在Web API中访问MVC控制器以从视图获取pdf?

How i access mvc controller in web api to get pdf from view?

注意:在Web api中声明的mvc控制器对象,以便它在"GetPdfBytesFormView"方法中给出"ControllerContext"为空.

Note: mvc controller object declared in web api so it gives "ControllerContext" is null in "GetPdfBytesFormView" method.

网络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控制器中编写以下代码

Let say your controller name is "PDFController" and action name is "GetPDF". Write following code in your api controller

    // 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应该会生成.

Your are done now. Your pdf should get generated.

希望有帮助

这篇关于如何在Web API控制器中访问MVC控制器以从视图获取pdf的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆