0

I get this error Multiple actions were found that match the request when I try to post on api/objects (invoke the last action in my code snippet here) I'm using web api 1. Althought I have more controllers and more Post actions around the web api, all of them have some simple inputs like string or int or combination od simple and complex data. This is only post action that receives only one parametar that is complex object, so ofcourse the only action that receives ObjectMainData model. This is my ObjectsController:

   [HttpGet] 
    public List<ObjectMainData> Get()
    {
        CompressResponse();
        return objectRepository.GetObjects(UserPK);
    }
    [HttpGet]


    public List<ObjectMainData> GetObject(string objectId)
    {
        CompressResponse();
        return objectRepository.GetObjects(UserPK, objectId);
    }
    [HttpPost] //Update
    [OnlineAuthorize]
    public ObjectCreatedResponse Post(string objectId,[FromBody]ObjectMainData objekt)
    {
      //  string objectId = objekt.Id;
        ObjectCreatedResponse objectIdAndCode = null;
        objectIdAndCode = objectRepository.Update(objekt);
        return objectIdAndCode;
    }

   //Insert  
    [HttpPost, ActionName("Post")] 
    public ObjectCreatedResponse Spremi(ObjectMainData objekt)
    {
        string objectId = objekt.Id;
        ObjectCreatedResponse objectIdAndCode=null;
        if (objectId == null)
        {
            objectIdAndCode= objectRepository.Insert(objekt,UserPK);
            return objectIdAndCode;

        }
        else
        { 
          objectIdAndCode=objectRepository.Update(objekt);
           return objectIdAndCode;

        }

This is relevant part of my web api route config:

        config.Routes.MapHttpRoute(  
             name: "objects/",
             routeTemplate: "api/objects",
             defaults: new { controller = "Objects" }
           );
        config.Routes.MapHttpRoute(
              name: "objects/{objectId}",
              routeTemplate: "api/objects/{objectId}",
              defaults: new { controller = "Objects" },
              constraints: new { objectId = @"\d+-\d+" }
            );

        config.Routes.MapHttpRoute(
           name: "api/objects/{objectId}/Attributes",
           routeTemplate: "api/objects/{objectId}/Attributes",
           defaults: new { controller = "ObjectAttributes" });


        config.Routes.MapHttpRoute(
          name: "api/objects/{objectId}/Images/{imageId}/Description",
          routeTemplate: "api/objects/{objectId}/Images/{imageId}/Description",
          defaults: new { controller = "Images", action = "Description" });


        config.Routes.MapHttpRoute(
         name: "api/objects/{objectId}/images",
         routeTemplate: "api/objects/{objectId}/Images",
         defaults: new { controller = "Images" });

        config.Routes.MapHttpRoute(
          name: "Units types api action selected",
          routeTemplate: "api/units/types/{objectId}",
          defaults: new { controller = "UnitsMisc", action = "Types" });

        config.Routes.MapHttpRoute(
           name: "api/objects/{objectId}/units/{unitId}",
           routeTemplate: "api/objects/{objectId}/units/{unitId}",
           defaults: new { controller = "Units" });


        config.Routes.MapHttpRoute(
        name: "images  api",
        routeTemplate: "api/objects/{objectId}/Units",
        defaults: new { controller = "Units" });


        config.Routes.MapHttpRoute(
         name: "api/objects/{objectId}/Images/sort",
         routeTemplate: "api/objects/{objectId}/Images/sort",
         defaults: new { controller = "Sort", action = "Images" });

      //SLIKA jedna
      config.Routes.MapHttpRoute(
        name: "api/objects/{objectId}/Images/{imageId}",
        routeTemplate: "api/objects/{objectId}/Images/{imageId}",
        defaults: new { controller = "Images" });


        config.Routes.MapHttpRoute(
                name: "DefaultApi",
                routeTemplate: "api/{controller}/{id}",
                defaults: new { id = RouteParameter.Optional });
Vlado Pandžić
  • 4,879
  • 8
  • 44
  • 75

1 Answers1

0

That's because you have two post actions, and the routing system has no way to choose one of them

You can do one of this things to avoid the problem:

  • use action names in your Web API routes and invocations, just like you do with MVC controllers and routing
  • change the method of the update: instead of POST use PUT. This is the REST semantics: PUT vs POST in REST

With any of these changes, it will start working.

Community
  • 1
  • 1
JotaBe
  • 38,030
  • 8
  • 98
  • 117
  • I really thought it will help, but it doesn't – Vlado Pandžić Apr 21 '15 at 09:43
  • Which is the new problem? You thought it would help, but I know that, if you do it in the right way, it does help ;) Please, update your question with what you've tried and what problem you're getting now. – JotaBe Apr 21 '15 at 09:45
  • I simply put [HttpPut] attribute on update method. Insert (problematic method) remained [HttpPost] – Vlado Pandžić Apr 21 '15 at 09:47
  • If I delete update method, insert method still not working with the same error. – Vlado Pandžić Apr 21 '15 at 09:50
  • event if you rename `Spremi` to `Post`? If so, look at the routes. They look too complex. In a big project I just have a handful of them, and it looks like you have too many for a few cases. Try to simpli fy this. – JotaBe Apr 21 '15 at 10:01
  • I'm trying to make restful api, but the problem is web api doesn't support attribute routing so config looks complex. – Vlado Pandžić Apr 21 '15 at 10:06
  • Don't kid yourself: you shpuld simplify your routes. It's not a question of having or not attribute routing. Do you really have under control all the routing system that you have implemented? Can you assure that your routes work for all your cases? Have you even tried them? Believe me, you're going to find a lot of trouble if you don't dimplify your routes. Or at least, if you don't test them. http://www.strathweb.com/2012/08/testing-routes-in-asp-net-web-api/ – JotaBe Apr 21 '15 at 11:10