[RESOLVED]ASP .Net MVC – how to call a controller method from an aspx page using ajax call – getting 404 (Not Found)


I have a ASP .Net MVC application, that has Customers controller with a
GetCustomers method which retrieves data .

************Customers API Controller***************

public class CustomersController : ApiController
{ public string GetCustomers()

I then created a aspx page and wanted to call the GetCustomers method using an ajax call.

************AJAX call ***************
<script type="text/javascript">
$(document).ready(function () {
                                                 var obj;
                                                 $("#mycontrol").click(function () {

                                                             type: "post",
                                                             contentType: "application/json",
                                                              url: "/api/GetCustomers",

When I run the app locally, the above ajax call fails with "POST http://localhost:14679/api/Customers
404 (Not Found)"

I have tried providing the path as /api/Customers, also, tried adding a route explicity [Route("api/Customers")]. But it still does not seem to locate the resource.

Any ideas what I am missing / doing wrong here?

I guess your controller Action should be decorated with HttpPost attribute 

public class CustomersController : ApiController


public string GetCustomers()


// your code



Try to add "[HttpPost]" like this (actually default is GET):

/*** Make sure you have the following "using" ***/
using System.Web.Http;

public class CustomersController : ApiController
    public string GetCustomers()

Have fun

I think it should be HttpPost as he/she mention type = "POST " in ajax xall. Correct me if im wrong

thanks all, but I am stillg getting the same error.

I added [HttpPost] decoration to the method, but if failed.

using System.Web.Http was already in place.

When I run the MVC application and try to access the method through fiddler I get POST http://localhost:14679/api/Customers 

HTTP 404. The resource you are looking for (or one of its dependencies) could have been removed, had its name changed, or is temporarily unavailable. &nbsp;Please review the following URL and make sure that it is spelled correctly.

[HttpException]: The controller for path '/api/Customers' was not found or does not implement IController.
at System.Web.Mvc.DefaultControllerFactory.GetControllerInstance(RequestContext requestContext, Type controllerType)
at System.Web.Mvc.DefaultControllerFactory.CreateController(RequestContext requestContext, String controllerName)
at System.Web.Mvc.MvcHandler.ProcessRequestInit(HttpContextBase httpContext, IController& controller, IControllerFactory& factory)
at System.Web.Mvc.MvcHandler.BeginProcessRequest(HttpContextBase httpContext, AsyncCallback callback, Object state)
at System.Web.Mvc.MvcHandler.BeginProcessRequest(HttpContext httpContext, AsyncCallback callback, Object state)
at System.Web.Mvc.MvcHandler.System.Web.IHttpAsyncHandler.BeginProcessRequest(HttpContext context, AsyncCallback cb, Object extraData)
at System.Web.HttpApplication.CallHandlerExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute()
at System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean& completedSynchronously)

Unless you have explicitly adjusted your routes, you might try specifying the method that you are targeting within your URL (e.g. /api/Customers/GetCustomers). Additionally, is there any reason that you are making an actual POST request instead
of a GET in this situation?

thanks, to get past this issue, I created a separate wep api cloud service application. 

MVC app page ajax -> calling api controller method on the new cloud service

With this set up, when I tried running it locally (the MVC app and the api cloud service), I ran into a CORS issue. I then confirgued the cloud service to use local IIS instead of IIS express and it worked. 

I am still trying to understand why the api method from within the MVC app controller does not work.

Leave a Reply