2017-11-14 13 views
0

aurelia-fetch-clientを使用してASP.NETコアWebアプリケーションにGuidsの配列を送信していますが、サーバー側ではモデルバインダーがそれを取得しませんnotificationIdsのリストはnullです。しかし、私がSwagger、またはCURLを通して要求を行うとき、それはちょうどうまく結びつきます。GuidedのモデルバインディングAsp.Netコアのリスト

GUIDの書式に問題があった場合に備えて、私のコントローラメソッドのシグネチャを変更して文字列のリストを受け入れるようにしましたが、同じ問題です。

JS

var body = {notificationIds : this.notifications.map(x => x.notificationId) }; 
    console.log("Dismissing All notifications"); 

    await this.httpClient.fetch('http://localhost:5000/api/notifications/clear', 
     { 
      method: 'POST', 
      body: json(body), 
      headers: { 
       'Authorization': `Bearer ${localStorage.getItem('access_token')}`, 
       'Accept': 'application/json', 
       'Content-Type': 'application/json', 
       'X-Requested-With': 'Fetch' 
      }, 
      mode: 'cors' 
     }).then(response => { 
      if(response.status == 204){ 
       //Success! Remove Notifications from VM 
      } 
      else{ 

       console.log(response.status) 
      } 
     }) 

コントローラメソッド

// POST: api/Notifications 
     [HttpPost] 
     [Route("clear")] 
     [ProducesResponseType((int)HttpStatusCode.NoContent)] 
     [ProducesResponseType((int)HttpStatusCode.BadRequest)] 
     public async Task<IActionResult> Post([FromBody]List<string> notificationIds) 
     { 
      if (notificationIds.IsNullOrEmpty()) 
      { 
       return BadRequest("No notifications requested to be cleared"); 
      } 

      var name = User.Claims.ElementAt(1); 

      await _notificationRepository.Acknowledge(notificationIds, name.Value); 

      return NoContent(); 
} 

興味深いのは、クロム(V62)は何も掲載示さないということです。 enter image description here

しかし、フィドラーは

enter image description here

+0

返されたデータは、VMのどのプロパティにも表示されないように設定していません。 –

+0

私はVM上で何も設定したくありません。それは、私はguidのリストを投稿すると、サーバー上のモデルのバインダーは、問題をそれらをピックアップしないという事実です – MrBliz

+0

ああ。私はその質問を完全に間違って読んだ。申し訳ありません。 –

答えて

1

を行いますが、JavaScriptから渡されたオブジェクトの形状は、あなたが期待するASP.NETフレームワークを言っているオブジェクトと同じ形状ではありません。

あなたがこの問題を解決することができ、2つの方法があります

オプション1:あなたはJavaScriptで は、var body = this.notifications.map(x => x.notificationId);にあなたの体を変える

オプション2: は、C#でオブジェクトを作成します。あなたがJavaScriptから渡しているものを反映しています。その後、

namespace Foo 
{ 
    public class Bar 
    { 
    public List<string> NotificationIds { get; set; } 
    } 
} 

とあなたのコントローラメソッドは、次のように更新します。

// POST: api/Notifications 
[HttpPost] 
[Route("clear")] 
[ProducesResponseType((int)HttpStatusCode.NoContent)] 
[ProducesResponseType((int)HttpStatusCode.BadRequest)] 
public async Task<IActionResult> Post([FromBody]Bar bar) 
{ 
    if (bar.NotificationIds.IsNullOrEmpty()) 
    { 
    return BadRequest("No notifications requested to be cleared"); 
    } 

    var name = User.Claims.ElementAt(1); 
    await _notificationRepository.Acknowledge(bar.NotificationIds, name.Value); 
    return NoContent(); 
} 
1

ここでの問題は、あなたがリストを含むプロパティを持つオブジェクトを送信しているGUIDのリストを送信していないということですGUIDの(peinearydevelopmentで記述されているように)ビューモデルを作成して使用するか、jsonオブジェクトを参照するdynamicパラメータを受け入れます。

public async Task<IActionResult> Post([FromBody] dynamic json) 
{ 
    var notificationIds = json.notifcationIds; 
    ... 
関連する問題