简体   繁体   中英

Passing json data list to HttpPost controller in ASP.NET Core 2.1

I have a model:

public class NotificationsDeliveryTypesIds
{
  public int NotificationGroupId { get; set; }
  public int DeliveryTypeId { get; set; }
  public int UserNotificationTypeDeliveryChoiceId { get; set; }
  public bool Selected { get; set; }
}

In my HomeController I have:

[HttpPost]
[AllowAnonymous]
public void SaveNotifications(List<NotificationsDeliveryTypesIds> selectedNotificationDeliveryTypesIds)
{
}

I put a debugger in my SaveNBotifications method, and I'm trying to call it from Postman , but I got:

400 error - bad request.

I am trying to pass in the body:

[{
"NotificationGroupId": 1,
"DeliveryTypeId": 2,
"UserNotificationTypeDeliveryChoiceId": 3,
"Selected":true
}]

When I put asp-controller and asp-home , it hits debugger. And I can see in my chrome debugger data like:

selectedDeliveryTypes: 1,1,0
selectedDeliveryTypes: 3,2,271
selectedDeliveryTypes: 4,2,272
selectedDeliveryTypes: 4,1,273
selectedDeliveryTypes: 5,2,0

I want to be able to hit breakpoint from my Postman.

Modify your controller action by decorating the parameter with the [FromBody] attribute.

[HttpPost]
[AllowAnonymous]
public void SaveNotifications([FromBody] List<NotificationsDeliveryTypesIds> selectedNotificationDeliveryTypesIds)
{
}

问题出在 AUthenticationCookie

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM