[SOLVED] Building a MultipartFormData API response


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){
    var responseContent = new MultipartFormDataContent();
    responseContent.Add(new StringContent(jsonString), 'JSON Object');
    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.


Return ResponseMessageResult, it supports IHttpActionResult:

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

Answered By – pakeha_by

Answer Checked By – Gilberto Lyons (BugsFixing Admin)

Leave a Reply

Your email address will not be published. Required fields are marked *