How to access mvc controller in web api controller to get pdf from view
Finally get solution.
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();
Your are done now. Your pdf should get generated.
Hope it help
Try by replacing your GetRequestionPdf with below one:
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; }