简体   繁体   中英

How to pass array of datetime from vue.js to an ASP.NET Core API?

My API code

[HttpGet("/GetBusinessDaysWithPublicHolidayDates/")]
public int GetBusinessDays(DateTime startDate, DateTime endDate,[FromQuery] IList<DateTime> publicHolidays)
{
    var noOfDays = _dayCalculatorService.BusinessDaysBetweenTwoDates(startDate, endDate, publicHolidays);

    return noOfDays;
}

My API call from Vue js code:

var arr = JSON.stringify(this.publicHolidays);

axios.get('https://localhost:7054/GetBusinessDaysWithPublicHolidayDates', {
    params: {
      startDate: this.startdate,
      endDate:this.enddate,
      publicHolidays:JSON.parse(arr)

URL is created in this format:

https://localhost:7054/GetBusinessDaysWithPublicHolidayDates?publicHolidays[]=2022-06-16T14:39:00.000Z&publicHolidays[]=2022-06-20T14:39:00.000Z

But API is working with this URL:

https://localhost:7054/GetBusinessDaysWithPublicHolidayDates?publicHolidays=2022-06-16T14:39:00.000Z&publicHolidays=2022-06-20T14:39:00.000Z

How to achieve this?

You can pass the date and time as an array using JSON.stringify.

this.$axios.get('/GetBusinessDaysWithPublicHolidayDates/', {
  params: {
    q: JSON.stringify(result)
  }
})

And URL for the API call will get created as

http://localhost:7054/cidade/?q=[value1,value2]

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