Anamnian
Anamnian

Reputation: 427

Calling controller method via ajax reach "404 not found"

Got that error while try to call my controller's method (controller's name is ProductsController):

public ActionResult GetProducts()
{
    return false;
}

Calling code like this:

$(document).ready(function () {
        $.ajax({
            type: 'POST',
            url: '@Url.Action("GetProducts", "ProductsController")',
            dataType: 'json',
            cache: false,
            contentType: 'application/json; charset=utf­8',
            data: JSON.stringify(""),
    })

Console in Chrome says:

jquery-1.10.2.js:8720 POST http ://localhost:56408/ProductsController/GetProducts 404 (Not Found)

Do you have any idea what is the problem?

Upvotes: 2

Views: 2767

Answers (1)

Nkosi
Nkosi

Reputation: 247123

Use the controller name prefix Products instead of ProductsController

$(document).ready(function () {
    $.ajax({
        type: 'POST',
        url: '@Url.Action("GetProducts", "Products")',
        dataType: 'json',
        cache: false,
        contentType: 'application/json; charset=utf­8',
        data: JSON.stringify(""),
});

Asp.Net-MVC uses a naming convention for controllers.

Upvotes: 2

Related Questions