NiceYellowEgg
NiceYellowEgg

Reputation: 562

POST Json to MVC Controller - No action was found on controller

I've got the following AJAX request:

var recordToRevert = $(this);
var umbrellaId = $(this).data("id");
var requestData = {
    umbrellaId: umbrellaId,
};

$.ajax({
    url: "/Express2/api/BenefitBank/RevertTransactionUmbrella",
    type: "POST",
    dataType: 'json',
    contentType: 'application/json; charset=utf-8',
    data: JSON.stringify(requestData),
    headers: {
        "Authorization-Token": "7,181,22",
        "CentreNumber": "0"
    }
}).done(function (data) {
    if (data.FoundRevertableTransactions) {
        recordToRevert.parent().html("<span class=\"success\">Reverted</span)");
    } else {
        alert('Umbrella NOT reverted');
    }
}).fail(function () {
    alert('Umbrella NOT reverted');
});

I'm sending it to an MVC controller/action here:

[HttpPost]
[ActionName("RevertTransactionUmbrella")]
public HttpResponseMessage PostRevertTransactionUmbrella(Int64 umbrellaId)
{
    if (ModelState.IsValid)
snip...

This is the response that i'm getting.

{"Message":"No HTTP resource was found that matches the request URI 'http://localhost/Express2/api/BenefitBank/RevertTransactionUmbrella'.","MessageDetail":"No action was found on the controller 'BenefitBank' that matches the request."}

I've set a breakpoint and the controller action is not being hit. I wonder if i'm not sending the JSON correctly?

This request works though:

$.ajax({
    url: "/Express2/api/BenefitBank/GetRequestObjects",
    type: "GET",
    dataType: 'json',
    contentType: 'application/json; charset=utf-8',
    //data: JSON.stringify(requestData),

[HttpGet]
[ActionName("GetRequestObjects")]
public HttpResponseMessage GetRequestObjects()
{
    return Request.CreateResponse(HttpStatusCode.Accepted, new BenefitBankRequestObjects());
}

It looks like the controller is being hit on both requests, however, the action is not being hit on the first?

Upvotes: 1

Views: 1551

Answers (2)

Akash Kava
Akash Kava

Reputation: 39916

Either you must pass a query string equivalent to Int64 umbrellaId or set its default value to zero.

Upvotes: 2

Oscar
Oscar

Reputation: 13960

Try changin the URL from:

url: "/Express2/api/BenefitBank/RevertTransactionUmbrella"

to:

url: "/Express2/api/BenefitBank/PostRevertSingleTransaction"

Upvotes: 0

Related Questions