且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

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

更新时间:2023-02-13 09:07:14

最后获得解决方案.

假设您的控制器名称是"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.

希望有帮助