Search code examples
.netc#-4.0asp.net-web-api2asp.net-web-api-routing

How to work with multiple Put and Post methods in Asp.net web Api 2


In my controller below I have 2 methods using Put and Patch verbs which I want to call on different times, as you can see I am trying to pass the json objectt but but when ever I am running the project and trying to call the api i am getting this error "Multiple actions were found that match the request"

I am sure I am messing the route but don't know what exactly where I am doing wrong, here is the controller code

[ApiVersion1RoutePrefix("tests")]
public class TestController : ApiController
{


       [Route("{}", Name = "PackageDropOffLocation")]
    [HttpPut]
    [HttpPatch]
    public IHttpActionResult PackageDropOffLocation(HttpRequestMessage, PackageDropOffLocationRequest packageDropOffLocationRequest)
    {
        return null;

    }

    [Route("", Name = "PackageOnBoard")]
    [HttpPut]
    [HttpPatch]
    public IHttpActionResult PackageBoarded(HttpRequestMessage requestMessage, PackageBoardedRequest packageBoardedRequest)
    {
        return null;
    }

}
  public class PackageBoardedRequest
{
    public string PackageId { get; set; }

}

  public class PackageDropOffLocationRequest
{
    public string Id { get; set; }
    public double Longitude { get; set; }
    public double Latitude { get; set; }

}

Solution

  • The problem was in the Route attributes:

    [ApiVersion1RoutePrefix("tests")]
    public class TestController : ApiController
    {
    
    
        [Route("PackageDropOffLocation/{}")]
        [HttpPut]
        [HttpPatch]
        public IHttpActionResult PackageDropOffLocation(HttpRequestMessage, PackageDropOffLocationRequest packageDropOffLocationRequest)
        {
            return null;
    
        }
    
        [Route("PackageOnBoard")]
        [HttpPut]
        [HttpPatch]
        public IHttpActionResult PackageBoarded(HttpRequestMessage requestMessage, PackageBoardedRequest packageBoardedRequest)
        {
            return null;
        }
    
    }
      public class PackageBoardedRequest
    {
        public string PackageId { get; set; }
    
    }
    
      public class PackageDropOffLocationRequest
    {
        public string Id { get; set; }
        public double Longitude { get; set; }
        public double Latitude { get; set; }
    
    }