Web API: Why am a getting a '500' error when accessing my REST endpoints for javascript?

For me, the solution was found by doing the following:

1. Add a 'Route' annotation above the endpoints

   [Authorize]
    public class MyAPIController : ApiController
    {
        [HttpPost]
        [Route("api/MyAPI/MyPostEndPoint")]
        public void MyPostEndPoint()
       {
       }

       [HttpGet]
       [Route("api/MyAPI/MyGetEndPoint")]
       public void MyGetEndPoint()
       {
       }
    }

2. When calling the endpoints from the js factory, my POST call we different to my GET command:

function executeGet(url, success, failure) {
            $http.get(url, {
                headers: { 'Authorization': 'Blah' }
            }).then(
            function (data) {
                return success(data);
            }, function (err) {
                return failure(err);
            });
        }

        function executePost(url, success, failure) {
            var req = {
                method: 'POST',
                url: url,
                headers: {
                    'Authorization': 'Blah']
                },
                data: { test: 'test' }
            };

            $http(req).then(function(data){
                return success(data);
                }, 
                function (err) {
                    return failure(err);
                }
            );
        }

Comments

Popular posts from this blog

SharePoint 2013: Error updating managed account credentials

Error deploying Nintex workflow: An item with the same key has already been added