1

I have an API Controller Action defined as:

public async Task<IHttpActionResult> ChangePassword(string userId, string password)

My original plan was to pass both userId and password through the data attribute of an AJAX request instead of through the API url.

E.g.:

$.ajax({
    url: "/api/users/resetpassword",
    data: JSON.stringify({
        "userId" : userId,
        "password" : password
    }),
    dataType: "json",
    method: "POST",
    success: function () {
        $("#ResetPasswordModal").modal('toggle');
        toastr.success("Password Reset");
    },
    error: function () {
        $("#ResetPasswordModal").modal('toggle');
        toastr.error("Password could not be reset");
    }

});

However, if I apply the Attribute Route [Route("api/users/resetpassword")]

I get the error

No action was found on the controller 'Users' that matches the request

If I then replace the attribute route with [Route("api/users/{userId}/resetpassword/{password}")], the application is able to find the ChangePassword action successfully.

When applying Attribute Routes to Controller Actions, is it required that all attributes are included in the Route?

3
  • 1
    You are missing the HttpPost attribute and you should also make sure attribute routing is enables Commented May 19, 2018 at 21:00
  • 1
    You should also create a model to hold the data and use the FromBody attribute Commented May 19, 2018 at 21:02
  • Attribute routing is enabled and I receive the above error even with the HttpPost attribute applied to the action Commented May 19, 2018 at 21:02

1 Answer 1

1

Create a model to hold the data sent from the client

public class ChangePasswordModel {
    public string userId { get; set; }
    public string password { get; set; }
}

update the action to expect the data in the body of the request

public class UsersController: ApiController {

    //...

    //POST api/users/resetpassword
    [HttpPost]
    [Route("api/users/resetpassword")]
    public async Task<IHttpActionResult> ChangePassword([FromBody]ChangePasswordModel data) {
        //...
    }

    //...
}

This assumes that attribute routing is already enabled in WebApiConfig.

config.MapHttpAttributeRoutes();

And is based on the following client details

url: "/api/users/resetpassword",
data: JSON.stringify({
    "userId" : userId,
    "password" : password
}),
dataType: "json",
method: "POST",
Sign up to request clarification or add additional context in comments.

Comments

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.