मैंने वेब एपी और एमवीसी एकल पृष्ठ वेब ऐप के लिए संयुक्त बनाया है। मैं वेब एपीआई को कॉल करना चाहता हूं और रोटेटिव एपीआई का उपयोग करके पीडीएफ बनाने के लिए एमवीसी नियंत्रक प्रस्तुत करना चाहता हूं। समस्या यह है कि जब मैं वेब एपीआई में एमवीसी नियंत्रक का उपयोग करता हूं तो यह काम नहीं करता है।पीडीएफ प्राप्त करने के लिए वेब एपीआई नियंत्रक में एमवीसी नियंत्रक का उपयोग कैसे करें
मैं पीडीएफ देखने के लिए वेब एपीआई में एमवीसी नियंत्रक का उपयोग कैसे करता हूं?
नोट: एमवीसी नियंत्रक ऑब्जेक्ट वेब एपीआई में घोषित किया गया है, इसलिए यह "GetPdfBytesFormView" विधि में "ControllerContext" शून्य है।
वेब एपीआई:
[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 वेब 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);
});
"यह काम नहीं है" का क्या अर्थ है? * किस तरह से * यह असफल हो जाता है? – David
@ डेविड, एमवीसी नियंत्रक ऑब्जेक्ट वेब एपीआई में घोषित किया गया है, इसलिए यह "GetPdfBytesFormView" विधि में "ControllerContext" शून्य है। –
जो सही होगा क्योंकि जब आपने इसे घोषित किया था तो संदर्भ सेट नहीं किया गया था। जब आप नियंत्रक को कॉल करते हैं तो फ्रेमवर्क आम तौर पर आपके लिए यह सब संभालता है लेकिन जैसा कि आप इसे मैन्युअल रूप से कर रहे हैं, यह सेट नहीं है। – Nkosi