Stack Overflow is a community of 4.7 million programmers, just like you, helping each other.

Join them; it only takes a minute:

Sign up
Join the Stack Overflow community to:
  1. Ask programming questions
  2. Answer and help your peers
  3. Get recognized for your expertise

I have made this project using this [Template](https://visualstudiogallery.msdn.microsoft.com/48d928e3-9b5c-4faf-b46f-d6baa7d9886c"this text appears when you mouse over").

It is a simple task manager, where you can create, complete, and delete a task.

I got it up and running and added an extra button, by almost completely copying the template author's code.

I following code is an example of his method and my method.

CODE

His way to complete a task:

frontend:

// called by ng-click="complete($index)"
$scope.complete = function(index)
    {
        $http.post('/api/WS_Todo/CompleteTodoItem/' + $scope.todoList[index].id)
            .success(function (data, status, headers, config) {
                $scope.getList();
            });
    }

backend:

[HttpPost]
    [Authorize]
    async public Task<HttpResponseMessage> CompleteTodoItem(int id)
    {
        var item = db.todos.Where(t => t.id == id).FirstOrDefault();
        if (item != null)
        {
            item.completed = true;
            await db.SaveChangesAsync();
        }
        return Request.CreateResponse(HttpStatusCode.Accepted);
    }

My way to reopen a task:

frontend:

// called by ng-click="reopen($index)"  
$scope.reopen = function(index)
    {
        $http.post('/api/WS_Todo/ReopenTodoItem', +$scope.todoList[index].id)
                .success(function (data, status, headers, config) {
                    $scope.getList();
                });
    }

backend:

 [HttpPost]
    [Authorize]
    async public Task<HttpResponseMessage> ReopenTodoItem(int id)
    {
        var item = db.todos.Where(t => t.id == id).FirstOrDefault();
        if (item != null)
        {
            item.completed = false;
            await db.SaveChangesAsync();
        }
        return Request.CreateResponse(HttpStatusCode.Accepted);
    }

PROBLEM

His code works as intended.

My code gives me:

POST http://localhost:33651/api/WS_Todo/ReopenTodoItem 404 (Not Found) angularjs:9734

And i can't see how the routing should be a problem when both methods are in the same frontent and backend controller.

share|improve this question

If i were you i would specify a route attribute on my webApi method:

[Route("/api/WS_Todo/CompleteTodoItem/{id}")]

And call the url from the front with the proper syntax. Same for the other methods.

share|improve this answer
    
Thanks, but my question wasn't just about getting it to work, i wish to understand why it doesn't work. Could you spread any light on that? – Kasper Sølvstrøm Oct 23 '15 at 14:40
    
I assume it doesn't make the matching between your controller method signature and the route you are calling from the front. It's often due to the method arguments naming or the $http.post route syntax, so you should try to specify a straight route, and call it exactly as it's specified. – Lokman Ersoy Oct 23 '15 at 14:53
    
Ok you still dont get it, His code works. my 90% identical code does not work. I can find no errors in my controller names, methods, URL's routing or anything. and yes, of course it is a problem of the matching between the front and backend, but my question remains, what is the problem? – Kasper Sølvstrøm Oct 23 '15 at 14:59

And after several attempts different approaches.. i was beaten by a comma in my url >.<

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.