Pass (Send) Array as parameter to Web API in ASP.Net Core MVC

pandeygolu4200
 
on Jun 14, 2022 11:06 PM
277 Views

How to pass array as parameter in asp.net core web api.

I have created a rest api, where i have to pass data as array in below format. but when i pass data using postman i am getting an error.

{ "notificationid": [
  "BC716385-5D65-4E0F-8987-070D6C42F629","B4425D6C-1028-4164-8C82-0BB9324F48D8"
]}

how can pass array in rest as parameter?

[HttpPost, Route("MarkAllAsRead")]
public async Task<IActionResult> NotificatiRead([FromBody]string [] NotificationId)
{
    try
    {
        ModelState.Remove("PatientId");
        if (!ModelState.IsValid)
        {
            return BadRequest(new
            {
                Status = false,
                Message = string.Join(Environment.NewLine, ModelState.Values
                                    .SelectMany(x => x.Errors)
                                    .Select(x => x.ErrorMessage))
            });
        }
        var res = await _notificationService.ReadNotificationlist(NotificationId);
        return res.Status ? StatusCode(StatusCodes.HTTP_OK, res) : StatusCode(StatusCodes.HTTP_DATANOTMATCHED, res);
    }
    catch (Exception ex)
    {
        Logger.AddErrorLog(ControllerContext.ActionDescriptor.ControllerName, "Notificationupdate", User.Identity.Name, ex);
        return StatusCode(StatusCodes.HTTP_INTERNAL_SERVER_ERROR, ex.Message);
    }
}

 

Download FREE API for Word, Excel and PDF in ASP.Net: Download
Mudassar
 
on Jun 15, 2022 01:08 AM

This way you can pass array.

You will need to make use of Anonymous dynamic objects and in order to Deserialize, you need to use Newtonsoft JSON library available at Nuget.

View

@{
    Layout = null;
}

<!DOCTYPE html>

<html>
<head>
    <meta name="viewport" content="width=device-width" />
    <title>Index</title>
    <style type="text/css">
        body { font-family: Arial; font-size: 10pt; }
    </style>
</head>
<body>
    <form id="form1">
    <script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"></script>
    <script type="text/javascript">
        $("body").on("click", "#btnSubmit", function () {
            var data = {};
            data.NotificationIds = new Array();
            data.NotificationIds.push("BC716385-5D65-4E0F-8987-070D6C42F629");
            data.NotificationIds.push("B4425D6C-1028-4164-8C82-0BB9324F48D8");
            $.ajax({
                url: '/api/AjaxAPI/PostData',
                type: 'POST',
                data: { "": JSON.stringify(data) },
                dataType: "json",
                success: function (response) {
                    alert(response.NotificationIds[0]);
                    alert(response.NotificationIds[1]);
                }
            });
        });
    </script>
</body>
</html>

Controller

using System.IO;
using System.Net;
using System.Net.Http;
using System.Web;
using System.Web.Http;
using Newtonsoft.Json;
namespace jQuery_Web_API_without_Model_MVC.Controllers
{
    public class AjaxAPIController : ApiController
    {
        [Route("api/AjaxAPI/PostData")]
        [HttpPost]
        public object PostData([FromBody] string json)
        {
            dynamic data = JsonConvert.DeserializeObject(json);
            return new { NotificationIds = data.NotificationIds };
        }
    }
}