0
votes

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 });
1

1 Answers

0
votes

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.