如何创建返回JSON的POST API方法?

时间:2014-04-18 00:32:20

标签: c# .net json asp.net-web-api

我是C#和.Net新手。我需要获得JSON响应而不是任务。我该怎么做?

我有一个带有POST方法的控制器,如下所示:

     public Task<HttpResponseMessage> PostDocument() {

              //I have some code here that downloads a multipart file 

        // Check if the request contains multipart/form-data.
        if (!Request.Content.IsMimeMultipartContent())
        {
            throw new HttpResponseException(HttpStatusCode.UnsupportedMediaType);
        }

        //String root = System.Web.HttpContext.Current.Server.MapPath("~/App_Data");
        var provider = new MultipartFormDataStreamProvider(root);

        // Read the form data and return an async task.
        var task = Request.Content.ReadAsMultipartAsync(provider).
            ContinueWith<HttpResponseMessage>(t =>
            {
                if (t.IsFaulted || t.IsCanceled)
                {
                    Request.CreateErrorResponse(HttpStatusCode.InternalServerError,   
                                                t.Exception);
                }


                // This illustrates how to get the file names.
                foreach (MultipartFileData file in provider.FileData)
                {
                    Console.WriteLine(file.Headers.ContentDisposition.FileName);
                    Console.WriteLine("Server file path: " + file.LocalFileName);

                }

                return Request.CreateResponse(HttpStatusCode.OK);



            });


        return task;

     }

我想返回JSON响应,如JSON数组或JSON对象,而不是返回task<>

0 个答案:

没有答案