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

前端 未结 2 1921
终归单人心
终归单人心 2021-02-09 02:08

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

2条回答
  •  天涯浪人
    2021-02-09 02:42

    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

提交回复
热议问题