0

I have GET, PUT, POST working in my WebAPI project. The last one of Http requests I am doing is DELeTE, BUT it does not work.

I have read through many posts in here as well as other websites, none of them. e.g.

WebAPI Controller is not being reached on DELETE command

WebAPI Delete not working - 405 Method Not Allowed

ASP.Net WebAPI Delete verb not working

ASP.NET Web API - PUT & DELETE Verbs Not Allowed - IIS 8

http://social.msdn.microsoft.com/Forums/en-US/windowsazuredevelopment/thread/8906fd7e-a60b-484e-be63-9574b9fca44a/

etc...

Are there any workarounds?

Please help, thanks.

Update:

My back-end code:

         [HttpDelete]
    public HttpResponseMessage Delete(int divisionID)
    {
        if (divisionID != default(int))
        {
            var found = dc.MedicareLocalAccounts.SingleOrDefault(m => m.DivisionID == divisionID);

            if (found == null)
            {
                return new HttpResponseMessage(HttpStatusCode.NotFound);
            }

            dc.MedicareLocalAccounts.Remove(found);

            dc.SaveChanges();

            return new HttpResponseMessage(HttpStatusCode.OK);
        }

        return new HttpResponseMessage(HttpStatusCode.NotFound);
    }

Now, if I change the parameter type from int to any classes, let's say Division

Delete(Division d)
{
     int divisionID = d.DivisionID;
     //....the rest is same
}

In this way, it works.

But I just do not want to input the entire object as a parameter to make the DELETE method work as it is not necessary.

So do you have any other better solutions?

Community
  • 1
  • 1
Franva
  • 5,410
  • 16
  • 62
  • 122
  • 1
    The questions you're linking to seem to have answers that have been accepted. As such "It seems a Microsoft code issue" would appear to be incorrect (since the askers of the questions have apparently succeeded now), and "Are there any workarounds" would seem to just be asking for reposts of those answers. – Damien_The_Unbeliever May 17 '13 at 06:16
  • 1
    _"It seems a Microsoft code issue"_ - nope. Do you think they forgot to implement delete? It most likely is an error in **your** code, which you don't show. – CodeCaster May 17 '13 at 06:31
  • What's your implementation and config – Joanna Derks May 17 '13 at 06:52
  • @Damien_The_Unbeliever The reason why I said it's a Microsoft issue is because I saw some solutions which I found in those links suggesting that to modify a system configuration file. Now I solved my problem, please see my question. – Franva May 17 '13 at 07:25
  • @CodeCaster Hi, please see my reply to Damien about why I think it is a Microsoft issue. – Franva May 17 '13 at 07:25
  • @JoannaTurban Hi, I attached my code please have a look, thx. – Franva May 17 '13 at 07:26
  • Those "system configuration files" are website configurations, where modules were loaded that intercepted DELETE and other requests, for example WebDAV does this. That does not mean this is a "Microsoft issue", more a "user error" because of not reading the manual. Anyway nice you got it solved. – CodeCaster May 17 '13 at 07:29
  • We'll need more to rule out an issue with your code - it definitely should not be required to have a complex object for DELETE, and I also don't believe it's MS issue. What does your request and route template look like? Also, what is actually the error you're getting - a 404 or something else? – Joanna Derks May 17 '13 at 08:06
  • A 405 would indicate a config issue, a 404 would indicate a routing issue. – Mike Wasson May 17 '13 at 15:10

2 Answers2

3

Web API handles simple parameter types (int) differently than complex types (classes). By default, a simple parameter is taken from the request URI, and a complex type is taken from the request body.

In your first example, the parameter name is 'divisionID' -- does this match your route variable? The default Web API route is "api/{controller}/{id}", so the parameter should be named 'id'.

Mike Wasson
  • 6,282
  • 2
  • 22
  • 20
  • simple and straight to the question. Thx !!! I didn't know that even the name should match the WebAPI route convention. – Franva May 18 '13 at 03:54
0

A workaround would be using the AttributeRouting library. This is an extension to WebAPI and can be downloaded from nuget. With the AttributeRouting library you could e.g. implement a function with HttpGet that wil perform the delete

[GET("delete/{id}"]
function DeleteThis(int id)
{
...
}
Raimond Kuipers
  • 1,138
  • 9
  • 17
  • Hi Raimond, thank you for your reply. I'd like to try it as the last solution as I do not want to make it work by using other libraries. – Franva May 17 '13 at 07:23