2016-07-25 6 views
0

C#Web APIで、JSON配列を含むPOST要求を受け入れようとしています。 JSON配列をLISTまたはILISTRegisterBindingModelクラスオブジェクトに逆シリアル化したいとします。コントローラのアクションでは、リストを繰り返して、目的のアクションを実行します。C#Web APIコントローラに送信されたJSON配列のHTTP要求が正しくありません。

Visual Studio 2015でASP.NET 5 Web Applicationテンプレートを使用しています。コントローラAccountRegisterListメソッドを追加しました。

これとは別に、私はC#コンソールアプリケーションでWebクライアントを作成しました。クライアントは、JSON配列を含むPOSTリクエストを送信します。

私が得る応答は常に400 - Bad Requestです。

RegisterListメソッドシグネチャのILISTまたはLISTにJSON配列を逆シリアル化する必要がありますか?私はJsonConverter.DeserializeObjectを使用しようとしましたが、IntelliSeneseはそのthe type name DeserializeObject does not exist in type JsonConverterと言います。

Visual Studioテンプレートで生成されるAPIドキュメントは、クライアントのJSON配列が正しくフォーマットされていることを示します。

次はRegisterListメソッドのコードです:

 // POST api/Account/RegisterList 
    [AllowAnonymous] 
    [Route("RegisterList")] 
    public async Task<IHttpActionResult> RegisterList(List<RegisterBindingModel> modelList) 
    { 
     if (!ModelState.IsValid) 
     { 
      return BadRequest(ModelState); 
     } 

     foreach (RegisterBindingModel model in modelList) 
     { 
      var user = new ApplicationUser() { UserName = model.Email, Email = model.Email }; 

      IdentityResult result = await UserManager.CreateAsync(user, model.Password); 

      if (!result.Succeeded) 
      { 
       return GetErrorResult(result); 
      } 

     } 
     return Ok(); 
    } 

次は、クライアントのコードです:

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Text; 
using System.Threading.Tasks; 
using System.Net.Http; 
using System.Net.Http.Headers; 
using Web_Client_Register_Account; 
using Newtonsoft.Json; 

namespace Web_Client_Register_Account 
{ 
class Program 
{ 
    static void Main(string[] args) 
    { 
     RunAsync().Wait(); 
    } 

    static async Task RunAsync() 
    { 
     using (var client = new HttpClient()) 
     { 
      Console.WriteLine("Hit any key"); 
      Console.ReadLine(); 
      client.BaseAddress = new Uri("http://localhost:9000/"); 
      client.DefaultRequestHeaders.Accept.Clear(); 
      client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json")); 





      var registrations = new List<Registration> { new Registration { Email = "[email protected]", Password = "Secrets1!", ConfirmPassword = "Secrets1!" }, new Registration { Email = "[email protected]", Password = "Secrets1!", ConfirmPassword = "Secrets1!" }, new Registration { Email = "[email protected]", Password = "Secrets1!", ConfirmPassword = "Secrets1!" }, new Registration { Email = "[email protected]", Password = "Secrets1!", ConfirmPassword = "Secrets1!" } }; 

    //HTTP Post - A JSON List in a Single POST 

      var registration_manifest = JsonConvert.SerializeObject(registrations); 

      Console.ReadLine(); 

       HttpResponseMessage response = await client.PostAsJsonAsync("api/Account/RegisterList", registration_manifest); 


       if (response.IsSuccessStatusCode) 
       { 
        Uri registrantUrl = response.Headers.Location; 
        Console.WriteLine(registrantUrl); 
        Console.ReadLine(); 
       } 
       Console.WriteLine(response); 
       Console.ReadLine(); 
     } 
    } 
} 
} 

答えて

0

HttpClient.PostAsJsonAsyncは、すでにJSONにエンコードし、そのJsonConvert.SerializeObjectだけ

をスキップ
HttpResponseMessage response = await client.PostAsJsonAsync("api/Account/RegisterList", 
                  registrations); 
+0

あなたは正しいです。あなたのソリューションは完璧に働いた、ありがとう。 –

関連する問題