0

I have an api, I'm sending multiple parameters here. url is as follows.

http://localhost:7150/api/logLoginDetails/GetLogLoginDetails?userId=&userIp=192.168.2.1&startTime=22.9.2020 18:07:13&endTime=23.9.2020 18:07:13

but when I switch to the api, only userIp information comes. The userId field should be null, but what should I do to ensure that the fields containing datetime are not null.

In the method below, only userIp information is filled.

 [RoutePrefix("api/logLoginDetails")]
    public class LogLoginDetailsController : ApiBaseController
    {
     [HttpGet, Route ("GetLogLoginDetails")]
             public HttpResponseMessage GetLogLoginDetails (int? userId = null, string userIp = null, DateTime? startTime = null, DateTime? endTime = null)
             {
                 try
                 {
                     return CROk (logLoginDetailsService.GetLogLoginDetails (userId: userId, userIp: userIp, startTime: startTime, endTime: endTime));
                 }
                 catch (Exception e)
                 {
                     return CRException (e);
                 }
             }
         }
Amazon
  • 406
  • 5
  • 14
  • You need to modify your parameters with `[FromQuery]` attribute – Eldar Sep 23 '20 at 15:22
  • 1
    @Eldar my project's not core project – Amazon Sep 23 '20 at 15:50
  • You pass parameter through query string and expect same mapping in action parameter, which do automatically mapped. You need to read it from QueryString of HttpContext or you may user FormQuery attribute. – m.k.sharma Sep 24 '20 at 04:37

1 Answers1

1

I believe the problem is related to the date format you are using to pass those values. The space between the date part and the hour part is replaced by %20 and the resulting text can't be parsed as a datetime.

You could try using this format: YYYY-MM-DDTHH:mm:SS

Like this:

http://localhost:7150/api/logLoginDetails/GetLogLoginDetails?userId=&userIp=192.168.2.1&startTime=2020-09-22T18:07:13&endTime=2020-09-23T18:07:13

Or, if you really need to use that format, maybe you could change the types to strings. Like this:

public HttpResponseMessage GetLogLoginDetails(int? userId = null, string userIp = null, string startTime = null, string endTime = null)
{
    try
    {
        return CROk(logLoginDetailsService.GetLogLoginDetails(userId: userId, userIp: userIp, startTime: startTime, endTime: endTime));
    }
    catch (Exception e)
    {
        return CRException(e);
    }
}
Ravicnet
  • 39
  • 5
  • its work string _startTime = startTime.HasValue ? startTime.Value.ToString(CultureInfo.InvariantCulture) : ""; – Amazon Sep 24 '20 at 10:47
  • 1
    I'm sorry, I don't follow. Where you are going to put that line? – Ravicnet Sep 24 '20 at 14:06
  • when i was pass parameters i did use _startTime = startTime.HasValue? startTime.Value.ToString (CultureInfo.InvariantCulture): ""; and create url for api – Amazon Sep 25 '20 at 17:40