简体   繁体   中英

How to pass Datetime parameter as string to web api controller (httpPut) PutAysnc

This is working: (but only date is getting passed to api)

public async Task ConfirmFLSANotice(string mechNbr, DateTime AcceptedDate)
        {
            await PutAsync("confirmflsanotice", mechNbr, AcceptedDate.ToString("yyyy-MM-dd"));
        }

Here i am passing just date, but i want to pass time also, but no other format is working except this("yyyy-MM-dd") . Tried multiple ways.

This is controller code:

[JwtAuthentication]
        [Route("api/login/confirmflsanotice/{mechNbr}/{AcceptedDate}/")]
        [HttpPut]
        public void ConfirmFLSANotice(string mechNbr, DateTime AcceptedDate)
        {
            var proxy = DsRegistry.Current.GetService<IUserDsService>();
            proxy.ConfirmFLSANotice(mechNbr, AcceptedDate);
            DsRegistry.Current.CloseService(proxy);
        }

please help, i can not change controller code.

You need to send the date in a format that the Binder can interpret. Just pass AcceptedDate.ToString("O") which convert date time to a ISO-8601 and this is the default format to pass date and time from one interface to other.

Also I recommend you to specify the type in the route, this avoids calls with invalid params:

[Route("api/login/confirmflsanotice/{mechNbr:int}/{AcceptedDate:datetime}/")]

我能够通过传递刻度,然后在后端将这些刻度转换为日期时间来解决该问题

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