找到了与请求web api匹配的多个操作

时间:2015-04-21 07:59:56

标签: asp.net-web-api asp.net-web-api-routing

当我尝试在Multiple actions were found that match the request上发帖时,我收到此错误api/objects(在此处调用我的代码段中的最后一个操作) 我使用web api 1.虽然我有更多的控制器和更多围绕web api的Post动作,但它们都有一些简单的输入,比如string或int或组合简单和复杂的数据。这只是一个只接收一个复杂对象的参数的post动作,所以当然只接收ObjectMainData模型的动作。 这是我的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;

        }

这是我的网络API路由配置的相关部分:

        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 个答案:

答案 0 :(得分:0)

那是因为你有两个后期操作,路由系统无法选择其中一个

你可以做其中一件事来避免这个问题:

  • 在Web API路由和调用中使用操作名称,就像使用MVC控制器和路由一样
  • 更改更新方法:而不是POST使用PUT。这是REST语义:PUT vs POST in REST

通过任何这些更改,它将开始工作。