WebAPI Controller is not being reached on DELETE command

前端 未结 3 813
隐瞒了意图╮
隐瞒了意图╮ 2020-12-24 13:40

I am having difficulty getting the DELETE Method on my Controller to fire when submitting the request over ASP.NET Web API. It returns a 404 but I cannot figure out why. The

相关标签:
3条回答
  • 2020-12-24 14:06

    Try returning HttpResponseMessage on your Delete method

    public HttpResponseMessage Delete( string id )
    {
      Customer cust = CustomerDb.Customers.Where(c => c.id == id).SingleOrDefault();
      if (cust == null)
        return new HttpResponseException( HttpStatusCode.NotFound ); // using NotFound rather than bad request
    
      CustomerDb.Customers.DeleteObject(cust);
      CustomerDb.SaveChanges();
      return new HttpResponseMessage( HttpStatusCode.NoContent );
    }
    
    0 讨论(0)
  • 2020-12-24 14:19

    You need to implement Delete method in Controller:

    // DELETE /api/values/5
    public void Delete(int id) {}
    
    0 讨论(0)
  • 2020-12-24 14:24

    Do you have this defined in your web.config?

       <system.webServer>
              <modules runAllManagedModulesForAllRequests="true">
              </modules>
        </system.webServer>
    
    0 讨论(0)
提交回复
热议问题