Web API CreateResponse created empty response

LuaLua

New Member
I was developing a simple file upload function with MVC Web API Controller. I wanted the response returned to the client to have some meaningful content. The code put custom object as part of the response. But the client always ended up with empty content even if the controller worked properly.Here is the code:\[code\]public class UploadController : ApiController{ [HttpPost] public Task<HttpResponseMessage> PostFormData() { // Check if the request contains multipart/form-data. if (!Request.Content.IsMimeMultipartContent()) { throw new HttpResponseException(HttpStatusCode.UnsupportedMediaType); } var provider = new CustomMultipartFormDataStreamProvider("\\\\remoteserver\\path"); // Read the form data and return an async task. var task = Request.Content.ReadAsMultipartAsync(provider). ContinueWith<HttpResponseMessage>(t => { if (t.IsFaulted || t.IsCanceled) { return Request.CreateErrorResponse(HttpStatusCode.InternalServerError, t.Exception); } // This illustrates how to get the file names. foreach (MultipartFileData file in provider.FileData) { Trace.WriteLine(file.Headers.ContentDisposition.FileName); Trace.WriteLine("Server file path: " + file.LocalFileName); } var response = Request.CreateResponse<TestObj>(HttpStatusCode.OK, new TestObj(1, "name")); return response; }); return task; }}\[/code\]The status code was 200 OK but the response was "{}" instead of the test object that I expected.Is there anything I missed out?Thanks
 
Back
Top