0

我想知道在 oData 服务中提供 pdf 的正确方法是什么。目前我使用 IHttpActionResult 作为返回类型,在使用 Fiddler 时效果很好。

webapi.config:

Dim HTML2Pdf = builder.Action("HTML2Pdf").Returns(Of IHttpActionResult)

控制器:

    <HttpPost>
    <ODataRoute("HTML2Pdf")>
    Public Function ActHTML2Pdf(<FromBody> parameters As ODataActionParameters) As IHttpActionResult
        Try
            Dim HTML2PDFObject As HTML2PDFObject = TryCast(parameters("HTML2PDFObject"), HTML2PDFObject)
            Dim HTML As String = WebUtility.HtmlDecode(HTML2PDFObject.HTML)

            Dim ms = New MemoryStream()
            Dim EOPDF_Result As HtmlToPdfResult = EO.Pdf.HtmlToPdf.ConvertHtml(HTML, ms)
            Dim buffer As Byte() = New Byte(-1) {}
            buffer = ms.ToArray()

            Dim result = New HttpResponseMessage(HttpStatusCode.OK) With {
                .Content = New StreamContent(New MemoryStream(buffer))
            }
            result.Content.Headers.ContentDisposition = New ContentDispositionHeaderValue("attachment") With {
                .FileName = "Document.pdf"
            }
            result.Content.Headers.ContentType = New MediaTypeHeaderValue("application/pdf")
            Dim response = ResponseMessage(result)

            Return response
        Catch e As Exception
            MyApplicationLog.Error(e.Message)
            Return BadRequest(e.Message)
        End Try
    End Function

但是当使用 Microsoft.Odata.Client 使用它时,我收到一个错误

消费客户:

Dim PDF = Container.HTML2Pdf(Html2PDFObject).GetValue

InvalidOperationException: The response payload is a not a valid response payload. Please make sure that the top level element is a valid Atom or JSON element or belongs to 'http://docs.oasis-open.org/odata/ns/data' namespace.

似乎 Odata 客户端只接受 xml 或 json 作为响应。

任何帮助表示赞赏。

4

0 回答 0