简体   繁体   中英

DELETE webapi asp.net not supported

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

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"

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM