Search code examples
c#angularjsodata

OData cannot find the controller unless the parameter is sent as int


I have this OData controller that works fine for GET and Patch method. However, there's a custom GET method that I had to create in order for use to load the data from a different server and copy to the new one from where the OData retrieves the data. When the user "Retrieves" new data, it sends the date as a parameter. Since it's a new claim for the date, I couldn't put the date in the $filter, hence the new GET method.

This is what the GET method looks like:

        [ODataRoute("Claims({key})")]
        [HttpGet]
        [EnableQuery(AllowedQueryOptions= System.Web.OData.Query.AllowedQueryOptions.All)]
        public HttpResponseMessage ClaimByDate([FromODataUri] int key)
        {
            System.Diagnostics.Debug.WriteLine(key);
            string date = key.ToString();
            DateTime claimDate = DateTime.ParseExact(date, "ddMMyyyy", CultureInfo.InvariantCulture);
            DateTime today = DateTime.Today;

            if (!ClaimsExistForToday(today))
            {
                GenerateClaimsList(today, claimDate);
                return Request.CreateResponse(HttpStatusCode.OK);
            }

            return Request.CreateResponse(HttpStatusCode.NotModified);

        }

Here's the angularjs controller

vm.retrieve = function () {

    if (vm.selectedDate !== null) {
        var day = vm.selectedDate.getDate();
        var date = (day + "" + (vm.selectedDate.getMonth() + 1) + "" + (vm.selectedDate.getFullYear()));

        ClaimService.getClaimsByDate(date)
            .then(function (response) { if (response) activate(); })

        vm.clear();
    }
}

Here's the angular function that's sending the get request:

function getClaimsByDate(date) { //Angularjs service

    return $http.get("odata/Claims(" + date + ")")
        .then(function (data) {

            return true;
        });
}

First Issue: The OData GET method have to have "int" for the parameter. I tried with string and DateTime, but when I send the data with 'string' as paramenter, it won't find the controller at all unless the data was sent as integer. And if I send the date as UTC, I get an error as potentially dangerous and won't execute.

Second Issue: Sending date as int, it will find the controller if date is sent as 30112015 in the format ddMMyyyy, but it won't find 1112015 or 01112015. It seems like it's only recognizing the date greater than 10. Other than this, everything else works fine including the regular GET method with [EnableQuery].


Solution

  • I found this SO answer Web API and OData- Pass Multiple Parameters which helped me out. I ended up creating a function with the key as parameter. Then it was only a matter of calling the function from Angular.