0

I tested my WebAPI2 (DELETE) in Fiddler and it is working fine but in my code had an error of Method not Allowed.

This is my Code :

    public async Task<bool> deleteUser(int id)
    {
        string URI = "http://api.danubeco.com/api/userapps";

        using (var client = new HttpClient())
        {   
            var response = await client.DeleteAsync(String.Format("{0}/{1}", URI, id));

            var myobject = await response.Content.ReadAsStringAsync();

            return Convert.ToBoolean(myobject);
        }            
    }

    // DELETE: api/userapps/5        
    [ResponseType(typeof(userapp))]
    public IHttpActionResult Deleteuserapp(int id)
    {
        userapp userapp = db.userapps.Find(id);
        if (userapp == null)
        {
            return NotFound();
        }

        db.userapps.Remove(userapp);
        db.SaveChanges();

        return Ok(userapp);
    }
Ryan Ayala
  • 77
  • 1
  • 8
  • Did you capture the request created by this code and compare it to the request you created in fiddler? I bet there's something different.. :) – CodingGorilla Mar 16 '16 at 18:01
  • @CodingGorilla in Fiddler i successfully delete a user using the same method but in code it response a Method not Allowed. – Ryan Ayala Mar 16 '16 at 18:04
  • I understand that, but did you look at headers of the requests, I suspect you're missing a header that it wants. – CodingGorilla Mar 16 '16 at 18:05
  • Are you using attribute based routing? If so, do you have the [HttpDelete] attribute on the method? – Bill Sambrone Mar 16 '16 at 18:06
  • Make sure you have a route in your WebApiConfig.cs that has action = "Delete" for your controller, since you don't appear to be using attribute based routing. – Kevin Mar 16 '16 at 18:12
  • I attached the Delete method of WebAPI. – Ryan Ayala Mar 16 '16 at 18:12
  • @CodingGorilla HTTP/1.1 200 OK - Header Response – Ryan Ayala Mar 16 '16 at 18:13
  • @Kevin what do you mean? – Ryan Ayala Mar 16 '16 at 18:13
  • @BillSambrone nope. aint using that because codes automatic generated by action w/ EF – Ryan Ayala Mar 16 '16 at 18:14
  • In your App_Start folder there should be a file named WebApiConfig.cs where you can define the routes for your controllers. Since I don't see any HTTP action attributes in your code I was assuming you are configuring your routes there. – Kevin Mar 16 '16 at 18:15
  • @Kevin i only have 2 Config Routes. 1. `routeTemplate: "api/{controller}/{id}"` and 2. `routeTemplate: "api/{controller}/{action}/{userid}/{password}"` – Ryan Ayala Mar 16 '16 at 18:18
  • `{StatusCode: 405, ReasonPhrase: 'Method Not Allowed', Version: 1.1, Content: System.Net.Http.StreamContent, Headers: { X-Powered-By-Plesk: PleskWin Date: Wed, 16 Mar 2016 18:21:45 GMT Server: Microsoft-IIS/8.0 X-Powered-By: ASP.NET Content-Length: 1015 Allow: GET Allow: HEAD Allow: OPTIONS Allow: TRACE Content-Type: text/html }}` – Ryan Ayala Mar 16 '16 at 18:22

2 Answers2

0

Try adding something like this:

        config.Routes.MapHttpRoute(
            name: "YourControllerApi",
            routeTemplate: "api/{controller}",
            defaults: new { controller = "YourControler", action = "Delete", id = RouteParameter.Optional }
        );

Of course you'll need to replace "YourController with the name of your controller class, and you may need to tweak the routeTemplate (this one assumes you'll call YourURL/api/YourController.

Kevin
  • 1,462
  • 9
  • 9
  • The only other place I know of where the allowed HTTP verbs are defined is in the Web.config under system.WebServer/handlers where it defines which ExtensionlessUrlHandler(s) to use. But those should have been setup when you created your WebApi project. – Kevin Mar 16 '16 at 18:54
0

I really don't know if this is a good practice but i modified the code like this.

    // DELETE: api/userapps/5   
    [HttpGet]
    [Route("api/userapps/deluser/{id}")]
    [ActionName("deluser")]
    [ResponseType(typeof(bool))]
    public bool Deleteuserapp(int id)
    {
        userapp userapp = db.userapps.Find(id);
        if (userapp == null)
        {
            return false;
        }

        db.userapps.Remove(userapp);
        db.SaveChanges();

        return true;
    }

    var response = await client.GetAsync(String.Format("{0}/{1}", URI, 

and used GetAsync rather than DeleteAsync.

Ryan Ayala
  • 77
  • 1
  • 8