2

I'm making a website with web API, I tried to send JSON to my controller but this is the error I keep getting.

 Multiple actions were found that match the request:
 Post on type AuctionWebsiteASP.Controllers.MovesController
 readDatabase on type AuctionWebsiteASP.Controllers.MovesController
 newItem on type AuctionWebsiteASP.Controllers.MovesController

First I tried searching for a fix but none of the fixes here helped.

My Controller

public class MovesController : ApiController
{
 [AcceptVerbs("GET", "POST")]
 public HttpResponseMessage Post([FromBody] Product product)
 {
 products.Add(product);
 newItem();
 return Request.CreateResponse(HttpStatusCode.OK, product);
 }
 }

My JS

$.ajax({
 type: "POST",
 dataType: "json",
 url: "/api/moves/",
 data: source,
 success: function (data) {
 $("#nStart").val(null);
 $("#nImg").val(null);
 $("#nMaker").val(null);
 $("#nModel").val(null);
 $("#nSerial").val(null);
 $("#nCpu").val(null);
 $("#nRam").val(null);
 $("#nGpu").val(null);
 $("#nStorage").val(null);
 $("#nBattery").val(null);
 $("#nDrivers").val(null);
 $("#nAccessories").val(null);
 $("#nNotes").val(null);
 console.log("Data has been sent!");
 },
 error: function (error) {
 jsonValue = jQuery.parseJSON(error.responseText);
 alert("ERROR!");
 }
});

Thanks in advance!

asked Nov 23, 2016 at 7:06
2
  • Is public HttpResponseMessage Post([FromBody] Product product) only method in the controller? Commented Nov 23, 2016 at 7:09
  • No, it isnt i tried using the Routes.MapHttpRoute( name: "DefaultApi", routeTemplate: "api/{controller}/{action}/{id}", defaults: new { id = RouteParameter.Optional } and added the action to the url but didnt help. Commented Nov 23, 2016 at 7:11

1 Answer 1

1

Your route is probably like this

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

But in order to have multiple actions with the same http method you need to provide webapi with more information via the route like so:

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

Try to use Route attribute to distinguish each action e.g

 public class MovesController : ApiController
 {
 [Route("Product")]
 public HttpResponseMessage Post([FromBody] Product product)
 {
 products.Add(product);
 newItem();
 return Request.CreateResponse(HttpStatusCode.OK, product);
 }
 }

$.ajax({ type: "POST", dataType: "json", url: "/api/moves/product", data: source, success: function (data) { $("#nStart").val(null); $("#nImg").val(null); $("#nMaker").val(null); $("#nModel").val(null); $("#nSerial").val(null); $("#nCpu").val(null); $("#nRam").val(null); $("#nGpu").val(null); $("#nStorage").val(null); $("#nBattery").val(null); $("#nDrivers").val(null); $("#nAccessories").val(null); $("#nNotes").val(null); console.log("Data has been sent!"); }, error: function (error) { jsonValue = jQuery.parseJSON(error.responseText); alert("ERROR!"); } });

answered Nov 23, 2016 at 7:15
6
  • then try to use Route attribute Commented Nov 23, 2016 at 7:17
  • What is the namespace for [Route("Product")] ? Commented Apr 4, 2017 at 8:42
  • Install-Package AttributeRouting (for MVC) Commented Apr 4, 2017 at 8:48
  • Install-Package AttributeRouting.WebApi (for Web API) Commented Apr 4, 2017 at 8:48
  • I am using ajax to call my post method in controllers. What should i use? Commented Apr 4, 2017 at 12:19

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.