0

I have a simple webApi app where I use the method GET POST and DELETE. The problem that I'm facing is with the DELETE operation. When the function header is not equal to the one for the POST it's not working. I'm getting the error

"The requested resource does not support http method DELETE."

Here is my controller code.

public class MyController : ApiController
{

    public List<JobInfo> Get()
    {
        //GET OPERATION
    }
    public void Post([FromBody]JToken body)
    {
        //POST OPERATION
    }

    public void Delete([FromUri]string id)
    {
        //DELETE OPERATION
    }
}

My server is configured like this.

public class Startup
{
    public void Configuration(IAppBuilder app)
    {
        app.UseCors(CorsOptions.AllowAll);
        HttpConfiguration configuration = new HttpConfiguration();
        configuration.Routes.MapHttpRoute(
            name: "DefaultApi",
            routeTemplate: "api/{controller}/{id}",
            defaults: new { id = RouteParameter.Optional }
        );
        app.UseWebApi(configuration);
    }
}

If I declare the DELETE operation like this:

    public void Post([FromBody]JToken body)

I get it running. The problem is that I need the id from uri (The API is defined like that). I searched and found this PUT and Delete not working with ASP.NET WebAPI and Database on Windows Azure

The problem is that I don't understand why that limitation and if I can break it. Thank you

Community
  • 1
  • 1
acostela
  • 2,597
  • 3
  • 33
  • 50
  • What limitation? In the question you linked to, the OP had a typo. If you specify that you want to extract a parameter whose name should be `id` in your route, your methods should have a parameter named `id`. BTW your `Get()` method doesn't accept any parameter. How are you going to GET a specific JobInfo object? – Panagiotis Kanavos Jan 20 '16 at 14:45
  • i have previously come across verb limitations on both the client and server side. Have a look into the X-HTTP-Method-Override header. This always gives you a way around it. web api doesnt support it out of the box but creating a message handler to support it is not too difficult – rdans Jan 20 '16 at 14:52
  • @PanagiotisKanavos I acces the DB and return the List. That operation is working as designed. My problem is Why I can't declare the Delete function with a different definition than the POST. If I declare POST and DELETE functions in the same way it's working but in DELETE I need to take the parameter from the URI not from the BODY. – acostela Jan 20 '16 at 15:03
  • I know it sounds stupid. Could you try with [HttpDelete] attribute and see it works? FYI: In my work, server admin doesn't want me to use DELETE and PUT either. – Win Jan 20 '16 at 15:38
  • @Win yes, I tried it. It's not working. Maybe the problem is that I can't pass parameters in the URI with a DELETE operation? – acostela Jan 20 '16 at 15:45

1 Answers1

1

Finally I solved it. The problem was basically the decorator in the parameter. If I don't use [FromUri] it's working and the function gets the parameter from the URL.

So finally I wrote.

    public void DeleteFromQueue(string id)
    {
         //DELETE operation
    }

and when I call

DELETE http://localhost:9000/api/controller/1223asd

I'm getting in id parameter the "1223asd"

acostela
  • 2,597
  • 3
  • 33
  • 50