4

In a new Web API project with :

  • GET method

    // GET api/values/5
    public string Get(int id)
    {
        return "value";
     }
    
  • DELETE method

    // DELETE api/values/5
    public void Delete(int id)
    {
        var client = new MongoClient("mongodb://localhost:27017");
        var server = client.GetServer();
        var db = server.GetDatabase("Test");
        var collection = db.GetCollection<Entity>("Entities");
        var deleteEntity = Query<Entity>.EQ(e => e.Id, id);
        collection.Remove(deleteEntity);        
    }
    

They have a similar URL: api/values/5.

When I want to call the Delete method, it executes the Get method. What do I do?

Nicolás Alarcón Rapela
  • 2,714
  • 1
  • 18
  • 29
Tanvir
  • 95
  • 1
  • 4
  • 11

4 Answers4

6

The URL is the same but you invoke this URL programaticaly with a "DELETE" 'http method' rather than "GET". If you are just navigating to the URL in your browser, the browser will only do a GET. How you programmatically do a DELETE (or POST or PUT) will depend on what library you are using to invoke the service but they all tend to have some kind of parameter or property called 'method' for setting this.

Robert Levy
  • 28,747
  • 6
  • 62
  • 94
3

Take a look at the HttpDelete attribute:

https://msdn.microsoft.com/en-us/library/system.web.mvc.httpdeleteattribute(v=vs.118).aspx

You need to decorate your methods like this so that MVC knows how to handle the incoming request:

[HttpGet]
public string Get(int id)
{
    ...
}

[HttpDelete]
public void Delete(int id)
{
    ...
}

If you're submitting to the delete method via an HTML form, bear in mind they only support the POST and GET methods, so you'll need to submit the DELETE via JavaScript:

http delete request from browser

Community
  • 1
  • 1
Rob Bell
  • 3,542
  • 5
  • 28
  • 49
0

Use...

Get: /controller/123456

Delete: /controller/id/123456

lem2802
  • 1,152
  • 7
  • 18
0

The config piece below solved my problem:

<validation validateIntegratedModeConfiguration="false" />
<handlers>
  <remove name="ExtensionlessUrlHandler-ISAPI-4.0_32bit" />
  <remove name="ExtensionlessUrlHandler-ISAPI-4.0_64bit" />
  <remove name="ExtensionlessUrlHandler-Integrated-4.0" />

  <!--This will enable all Web API verbose-->
  <add name="ExtensionlessUrlHandler-ISAPI-4.0_32bit" path="*." verb="GET,HEAD,POST,DEBUG,PUT,DELETE,PATCH,OPTIONS" modules="IsapiModule" scriptProcessor="%windir%\Microsoft.NET\Framework\v4.0.30319\aspnet_isapi.dll" preCondition="classicMode,runtimeVersionv4.0,bitness32" responseBufferLimit="0" />
  <add name="ExtensionlessUrlHandler-ISAPI-4.0_64bit" path="*." verb="GET,HEAD,POST,DEBUG,PUT,DELETE,PATCH,OPTIONS" modules="IsapiModule" scriptProcessor="%windir%\Microsoft.NET\Framework64\v4.0.30319\aspnet_isapi.dll" preCondition="classicMode,runtimeVersionv4.0,bitness64" responseBufferLimit="0" />
  <add name="ExtensionlessUrlHandler-Integrated-4.0" path="*." verb="GET,HEAD,POST,DEBUG,PUT,DELETE,PATCH,OPTIONS" type="System.Web.Handlers.TransferRequestHandler" preCondition="integratedMode,runtimeVersionv4.0" />
</handlers>
Lauren Rutledge
  • 1,195
  • 5
  • 18
  • 27
nabukhas
  • 193
  • 1
  • 9