0

I am trying to invoke DELETE method of ASP.net WEB API from jqGrid. However I get message - "Error Status: 'Method Not Allowed'. Error code: 405".

Below are the details.

JQGrid code-

 $("#employeeSalarysGrid").jqGrid('navGrid', '#employeeSalarysPager',
            {
                add: true,
                edit: true,
                del: true
            },
            editOption,
            addOption,
            delOption);

var delOption = {
        caption: "Delete",
        msg: "Delete selected record(s)?",
        bSubmit: "Delete", bCancel: "Cancel",
        mtype: "DELETE",
        url: 'http://localhost:50570/api/Test/'
    };

ASP.NET Web API method:

public HttpResponseMessage Delete(string id)
{
    //code for processing delete request
}

I have traced the request sent using Fiddler. Below is the request details:

DELETE http://localhost:50570/api/Test HTTP/1.1
Accept: */*
Content-Type: application/x-www-form-urlencoded; charset=UTF-8
Referer: http://localhost:53055/Views/Test.aspx
Accept-Language: en-US,en;q=0.5
Accept-Encoding: gzip, deflate
User-Agent: Mozilla/5.0 (Windows NT 6.3; WOW64; Trident/7.0; rv:11.0) like Gecko
Host: localhost:50570
Content-Length: 13
Connection: Keep-Alive
Pragma: no-cache

oper=del&id=2

Please note parameter Id=2 is sent in the body of request and not query string, which I believe should be fine.

Please let me know your thoughts on reason why delete method is not getting invoked.

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
AbSharp
  • 119
  • 2
  • 6
  • 20

2 Answers2

2

You should remove the body from DELETE request and append id at the end of the URL. The answer (or this old one) describes the implementation details. I would strictly recommend you to remove http://localhost:50570 prefix from URLs which you use and use for example url: '/api/Test/' instead of url: 'http://localhost:50570/api/Test/'. See here for restrictions of Ajax.

Community
  • 1
  • 1
Oleg
  • 220,925
  • 34
  • 403
  • 798
  • Thanks Oleg. The change you suggested helps, however method is still not getting invoked and error displayed is - "Error Status: 'Not Found'. Error code: 404". It gets invoked when I provide url as 'http://localhost:50570/api/Test/Delete/' + encodeURIComponent(postdata). Is there something wrong with asp.net web api signature? Note: Removing url prefix doesnt help either. – AbSharp Apr 07 '14 at 10:34
  • @Abhilash: Ajax request failed if you would make the request from site with URL other as `http://localhost:50570/...` because of security reasons. So one uses typically URLs like `/api/Test/'`. If you get 'Not Found' error then you can use Developer Tools of IE or Chrome (press F12 to start), Network tab to trace HTTP requests. Then you would see the difference in URL **actually used** (compare non-working URL `/api/Test/Delete` or `api/Test/Delete` case with working `http://localhost:50570/api/Test/Delete/'`). I recommend to find a fix or relative URL which work. – Oleg Apr 07 '14 at 11:53
  • That's one issue. But main issue is I need to mention '/Delete/' in URL. URL 'http://localhost:50570/api/Test/123' with mtype: "DELETE" doesn't invoke Delete web API method, instead I need to provide URL as 'http://localhost:50570/api/Test/Delete/123'. – AbSharp Apr 07 '14 at 12:47
  • @Abhilash: I'm not sure which problem you have currently. You don't specified which one ASP.NET Web API you used and which routing you use. Probably you should add `[HttpDelete]` and `[Route("api/Test")]` attributes to fix routing problems. You should divide client side problems from your server side problems. **If Fiddler shows that jqGrid send correct DELETE request to correct URL then your client problems are solved already.** You should examine your server side (for example routing inside of ASP.NET application) to solve your other problems. – Oleg Apr 07 '14 at 16:48
  • Thanks, figured out. Issue was with WebApiConfig.cs. – AbSharp Apr 07 '14 at 17:20
  • Thanks Oleg. Regarding locahost:port prefix issue, I am running ASP.net website as separate Visual Studio Solution and ASP.net Web API as separate. So essentially website is running on http://localhost:53055/ whereas web api is running on http://localhost:50570/. Hence in debug environment providing only relative URL as '/api/Test/Delete' wont work. Any thoughts, on how can I run website and web api on same local port, so that relative url works? – AbSharp Apr 08 '14 at 16:13
0

Since you use a simple type (string) as the method parameter, Web API is not able to route the request. Set the id in the URI like this - http://localhost:50570/api/Test/123. Alternatively, change the parameter to a complex type (class) and Web API will start routing correctly, binding body to the action method parameter.

  • Thanks Badri, now i am passing id in URI, however still method is not getting invoked when uri: http://localhost:50570/api/Test/123. Delete method gets invoked only when i provide uri as http://localhost:50570/api/Test/Delete/123. Do i need to modify asp.net web api? – AbSharp Apr 07 '14 at 10:38