0

I have this C# WebAPI 2 project:

[RoutePrefix("api/Account")]
public class AccountController : ApiController
{
    ....

    [HttpDelete]
    [Route("Delete")]
    public async Task<IHttpActionResult> Delete(string password)
    {
        // retrieve the authorized user's username
        // delete the authorized user using his password and username
    }

    ....
}

My HTML form:

  <form class="form-login span4 offset4 register-box" role="form">
    <h2 class="form-login-heading">Delete your account</h2>
    <h2 class="form-login-heading">Warning: this is permanent.</h2>
    <input type="password" class="form-control" placeholder="Password" data-ng-model="deleteConfirmPassword" required>
    <button class="btn btn-lg btn-info btn-block" type="submit" data-ng-click=" deleteAccount()">Delete</button>
</form>

And the controller that's used:

app.controller('accountController', [
    '$scope', '$location', '$http', 'authService',
    function($scope, location, $http, authService) {

        $scope.deleteConfirmPassword = "";

        $scope.deleteAccount = function() {
            $http.delete(authService.serviceBase + 'api/account/Delete/', {
                password: $scope.deleteConfirmPassword
            }).success(function() {
                authService.logOut();
            }).
            error(function(response) {
                var errors = [];
                for (var key in response.data.modelState) {
                    for (var i = 0; i < response.data.modelState[key].length; i++) {
                        errors.push(response.data.modelState[key][i]);
                    }
                }
                $scope.message = "Failed to delete your account due to:" + errors.join(' ');
            });
        };
    }
]);

According to Firebug, the correct URL is called, however no parameters appear to be passed, and I'm getting a 404 error. What am I doing wrong in this code?

0

1 Answer 1

1

The second parameter of $http.delete() is a config object, not a data object, so you need to wrap your parameters in another layer. Also, your URL should not actually contain "Delete". That's indicated by the HTTP verb:

$http.delete(authService.serviceBase + 'api/account/', {
    data: { password: $scope.deleteConfirmPassword }
})
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks, I read your link and I made a beginner mistake. However, I'm still getting a 404 error.
@ohyeah I'm curious why you're using [HttpDelete] and [Route("Delete")]. The point of Web API is that it's RESTful and the actions are indicated by the HTTP verbs and the the method names in your controller class. Please try removing those attributes and removing Delete/ from your URL. One additional thing to try is renaming your method from Delete to DeleteUser (or something like that). The method names are supposed to be of the form [verb]..., not just [verb].

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.