2 Replies - 440 Views - Last Post: 12 September 2016 - 03:15 PM

#1 wtp  Icon User is offline

  • D.I.C Regular

Reputation: 26
  • View blog
  • Posts: 319
  • Joined: 08-December 11

WebAPI Delete with parameter

Posted 12 September 2016 - 12:55 PM

I'm trying to make a delete function in my traineecontroller. The Get and Post in my traineecontroller work fine. However when I try to delete I receive a 405 Method not allowed. I put a breakpoint in the delete function and it is never called.

I think it might have to do with the parameter im accepting in the delete function or the route. But I didn't see anything wrong.

Calling it with httpdelete request to this address
http://localhost/MyProjectLogic/api/trainee/14


TraineeController.cs
using ProjectLiberate.Logic.Business;
using ProjectLiberate.Logic.Business.BusinessModels;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Threading.Tasks;
using System.Web.Http;

namespace ProjectLiberate.Logic.Api.Controllers
{
  public class TraineeController : ApiController
  {
    private readonly LogicHelper biz = new LogicHelper();

    public async Task<HttpResponseMessage> Get()
    {
      return Request.CreateResponse(HttpStatusCode.OK, await biz.GetAllTrainees());
    }

    [HttpPost]
    public HttpResponseMessage AddTrainee([FromBody] TraineeDto traineeDto)
    {
      if (traineeDto==null||traineeDto.BatchID <= 0 || String.IsNullOrEmpty(traineeDto.Email) || String.IsNullOrEmpty(traineeDto.FirstName) || String.IsNullOrEmpty(traineeDto.LastName) || String.IsNullOrEmpty(traineeDto.PhoneNumber))
      {
        var message = "You are missing information or are passing null";
        HttpError err = new HttpError(message);
        return Request.CreateResponse(HttpStatusCode.NotAcceptable, err);
      }
      else
      {
        bool added = biz.AddTrainee(traineeDto);
        return Request.CreateResponse(HttpStatusCode.OK, added);
      }
    }

    public void Put()
    {
      return;
    }

    [HttpDelete]
    public void Delete( int id)
    {
      biz.DeleteTrainee(id);
      return;
    }
  }
}

WebApi.config
using ProjectLiberate.Logic.Business;
using ProjectLiberate.Logic.Business.BusinessModels;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Threading.Tasks;
using System.Web.Http;

namespace ProjectLiberate.Logic.Api.Controllers
{
  public class TraineeController : ApiController
  {
    private readonly LogicHelper biz = new LogicHelper();

    public async Task<HttpResponseMessage> Get()
    {
      return Request.CreateResponse(HttpStatusCode.OK, await biz.GetAllTrainees());
    }

    [HttpPost]
    public HttpResponseMessage AddTrainee([FromBody] TraineeDto traineeDto)
    {
      if (traineeDto==null||traineeDto.BatchID <= 0 || String.IsNullOrEmpty(traineeDto.Email) || String.IsNullOrEmpty(traineeDto.FirstName) || String.IsNullOrEmpty(traineeDto.LastName) || String.IsNullOrEmpty(traineeDto.PhoneNumber))
      {
        var message = "You are missing information or are passing null";
        HttpError err = new HttpError(message);
        return Request.CreateResponse(HttpStatusCode.NotAcceptable, err);
      }
      else
      {
        bool added = biz.AddTrainee(traineeDto);
        return Request.CreateResponse(HttpStatusCode.OK, added);
      }
    }

    public void Put()
    {
      return;
    }

    [HttpDelete]
    public void Delete( int id)
    {
      biz.DeleteTrainee(id);
      return;
    }
  }
}

This post has been edited by wtp: 12 September 2016 - 12:57 PM


Is This A Good Question/Topic? 0
  • +

Replies To: WebAPI Delete with parameter

#2 andrewsw  Icon User is offline

  • lashings of ginger beer
  • member icon

Reputation: 6340
  • View blog
  • Posts: 25,570
  • Joined: 12-December 12

Re: WebAPI Delete with parameter

Posted 12 September 2016 - 01:03 PM

Did you search, something like "web api delete 405 method not allowed"? For example,

WebAPI Delete not working - 405 Method Not Allowed
Was This Post Helpful? 1
  • +
  • -

#3 wtp  Icon User is offline

  • D.I.C Regular

Reputation: 26
  • View blog
  • Posts: 319
  • Joined: 08-December 11

Re: WebAPI Delete with parameter

Posted 12 September 2016 - 03:15 PM

That worked! Ultra thanks
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1