【问题标题】:List of Guids not Model Binding Asp.Net Core没有模型绑定 Asp.Net Core 的 Guid 列表
【发布时间】:2018-04-27 15:53:27
【问题描述】:

我正在使用 aurelia-fetch-client 向 ASP.NET Core Web 应用程序发送一组 Guid,但是在服务器端,模型绑定器没有接收到它,notificationIds 的列表是 @987654326 @。但是,当我通过 Swagger 或 CURL 发出请求时,它绑定得很好。

我更改了我的控制器方法的签名以接受字符串列表,以防 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();
}

有趣的是 Chrome (V62) 没有显示任何帖子。

但是提琴手可以

【问题讨论】:

  • 据我所知,您没有将返回的数据设置为 VM 上的任何属性。
  • 我不想在虚拟机上设置任何东西。事实是,当我发布 guid 列表时,服务器上的模型绑定器没有拾取它们,这就是问题
  • 啊。我完全误解了这个问题。对此感到抱歉。
  • 没问题....:)
  • 看到这很有趣。 Chrome 没有显示任何已发布的内容,但 fiddler 会显示......

标签: asp.net asp.net-core asp.net-core-mvc aurelia


【解决方案1】:

您从 JavaScript 传递的对象的形状与您告诉 ASP.NET 框架期望的对象的形状不同。

有两种方法可以解决此问题:

选项 1: 在您的 JavaScript 中,将您的正文更改为 var body = this.notifications.map(x =&gt; 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();
}

【讨论】:

    【解决方案2】:

    这里的问题是您发送的不是 GUID 列表,而是发送具有包含 GUID 列表的属性的对象。创建并使用视图模型(如 peinearydevelopment 所述)或接受引用 json 对象的 dynamic 参数。

    public async Task<IActionResult> Post([FromBody] dynamic json)
    {
        var notificationIds = json.notifcationIds;
        ...
    

    【讨论】:

      猜你喜欢
      • 2021-05-20
      • 2020-03-14
      • 2021-09-21
      • 2017-04-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-09-21
      相关资源
      最近更新 更多