简体   繁体   中英

jQuery ajax - call various MVC controller actions

I have a controller ( RequestsController ) with several actions (all taking one parameter)

For example:

  • public List<Models.AcceptRequests> GetAllAcceptRequests(int passedId)
  • public List<Models.Requests> GetAllRequests(int passedId)
  • public List<Models.CancelRequests> GetAllCancelRequests(int passedId)

I am calling each of these actions from jQuery ajax as follows:

  var s = { passedId: 0, } $.ajax({ url: "../api/Requests/GetAllAcceptRequests", type: "GET", dataType: 'json', data: JSON.stringify(s), contentType: "application/json; charset=utf-8", success: function (response) { vm.Requests(ko.utils.unwrapObservable(ko.mapping.fromJS(response))); } }); 

I receive the following error:

Multiple actions were found that match the request: System.Collections.Generic.List1 [TCMSWeb.Models.Requests] GetAllRequests(Int32)

It is my understanding that you can call a controller action regardless of the parameters conflicting with other actions. Is this not the case then?

How can I call each controller action without having to specify a unique number of parameters for each?

EDIT:

  public static void RegisterRoutes(RouteCollection routes) { routes.IgnoreRoute("{resource}.axd/{*pathInfo}"); 
        routes.MapRoute(
            name: "Default",
            url: "{controller}/{action}/{id}",
            defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
        );
    }

It is my understanding that you can call a controller action regardless of the parameters conflicting with other actions

No, this understanding is wrong. This only makes the C# compiler happy but is invalid in ASP.NET MVC. You cannot have 2 controller actions with the same name on the same controller accessible with the same HTTP verb regardless of the parameters they are taking.

Change the Register method in WebApiConfig.cs like this.

routes.MapHttpRoute(
    name: "ActionApi",
    routeTemplate: "api/{controller}/{action}/{id}",
    defaults: new { id = RouteParameter.Optional }
);

This will enable RPC style routing. Also, change your jQuery call, like this. 1234 is just hard-coded data which will be bound to passedId parameter.

$.ajax({
        url: "../api/Requests/GetAllAcceptRequests?passedid=1234",
        type: "GET",
        dataType: 'json',
        success: function (response) {
            vm.Requests(ko.utils.unwrapObservable(ko.mapping.fromJS(response)));
        }
    });

You can also use url: "../api/Requests/GetAllAcceptRequests/1234" , if you change the route mapping to

routes.MapHttpRoute(
    name: "ActionApi",
    routeTemplate: "api/{controller}/{action}/{passedid}",
    defaults: new { passedid = RouteParameter.Optional }
);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM