locked
Passing Two Dates to WebApi RRS feed

  • Question

  • User1122355199 posted

    Hello everyone and thanks for your help in advance.  I need to develop a WebApi that will generate a report based on a date range that will be passed from a page via an ajax call.  The page will have a user input BeginningDate and EndingDate.  I'm really not sure the best way to go about this, especially given the difficulties passing dates to a WebApi via ajax.  Any help would be appreciated.

    Wednesday, June 7, 2017 2:00 AM

Answers

  • User1967761114 posted

    Hi kmcnet,

    According to your description, when you want to pass datetime parameter, you just make sure to pass a string that such like date format to the Web API server.

     See the following code:

    (1) Web API (server):

    public class CustomerController : ApiController
    { 
        [HttpPost]
        public string GetList(DateTime startTime, DateTime endTime)
        {
            return "StartTime=" + startTime.ToString("yyyy/MM/dd") + " EndTime=" + endTime.ToString("yyyy/MM/dd");
        }
    }

    (2) Ajax call (client):

    //you also could use such as 2017/01/01 13:00:00,2017-01-01 14:00:00 and so on,that's all ok when the string just like the date format.
    $.ajax({
        url: "http://localhost:61072/api/customer/getlist?startTime=2017-01-01&endTime=2017/06/08",
        type: "POST",
        success: function (result) {
            alert(result);
        }
    })

    However, I just show one of way to pass parameter for you, there has a lot ways to pass parameter, you could refer to the following link:

    https://docs.microsoft.com/en-us/aspnet/web-api/overview/formats-and-model-binding/parameter-binding-in-aspnet-web-api

    If you have any other questions, please feel free to contact me any time.

    Best Regards

    Even

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, June 8, 2017 1:55 AM

All replies

  • User1967761114 posted

    Hi kmcnet,

    According to your description, when you want to pass datetime parameter, you just make sure to pass a string that such like date format to the Web API server.

     See the following code:

    (1) Web API (server):

    public class CustomerController : ApiController
    { 
        [HttpPost]
        public string GetList(DateTime startTime, DateTime endTime)
        {
            return "StartTime=" + startTime.ToString("yyyy/MM/dd") + " EndTime=" + endTime.ToString("yyyy/MM/dd");
        }
    }

    (2) Ajax call (client):

    //you also could use such as 2017/01/01 13:00:00,2017-01-01 14:00:00 and so on,that's all ok when the string just like the date format.
    $.ajax({
        url: "http://localhost:61072/api/customer/getlist?startTime=2017-01-01&endTime=2017/06/08",
        type: "POST",
        success: function (result) {
            alert(result);
        }
    })

    However, I just show one of way to pass parameter for you, there has a lot ways to pass parameter, you could refer to the following link:

    https://docs.microsoft.com/en-us/aspnet/web-api/overview/formats-and-model-binding/parameter-binding-in-aspnet-web-api

    If you have any other questions, please feel free to contact me any time.

    Best Regards

    Even

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, June 8, 2017 1:55 AM
  • User1122355199 posted

    Thanks for the help.

    Saturday, June 17, 2017 2:44 AM