Home > other >  Building a MultipartFormData API response
Building a MultipartFormData API response

Time:03-21

I need to build an API that returns a JSON object and attached files in byte array form. So to do this I thought of using MultipartFormDataContent class in C#. I have the API written something like this.

public async Task<IHttpActionResult> MethodName(Params){
    ......statements......
    var responseContent = new MultipartFormDataContent();
    ......statements......
    responseContent.Add(new StringContent(jsonString), 'JSON Object');
    ......statements......
    responseContent.Add(byteArrayContent);
    return responseContent;
}

This is giving an error while building the project about not being able to convert the responseContent to IHttpActionResult. So I tried explicit casting like so.

return (IHttpActionResult)responseContent;

But this throws a runtime error about not being able to convert to IHttpActionResult. So how would I go about returning responseContent. Any help is appreciated.

CodePudding user response:

Return ResponseMessageResult, it supports IHttpActionResult:

return new ResponseMessageResult(new HttpResponseMessage() { Content = responseContent })
  • Related