sekhar
sekhar

Reputation: 23

HTTP Error 405 Method not allowed while calling web api delete method?

I am trying to call from MVC controller application to WEBAPI applicaiton method. I am getting error 405 method not allowed. Its working fine while calling GET and POST.

MVC Applicaiton:

[HttpPost]
public HttpResponseMessage DeleteService(int id) {
        //code for webapi
    }

WEB API application:

[HttpDelete]
public HttpResponseMessage DeleteService(int id) {
        try {
            ServicesModel service = dbContext.Services.Find(id);
            IEnumerable<ServicePropertiesModel> serviceProperies = dbContext.ServiceProperties.Where(x => x.ServiceId == id);
            if (service != null) {

                foreach (ServicePropertiesModel s in serviceProperies) {
                    dbContext.ServiceProperties.Remove(s);
                }
                dbContext.Services.Remove(service);
                dbContext.SaveChanges();
            }
            return Request.CreateResponse(HttpStatusCode.OK);
        } catch {
            return Request.CreateResponse(HttpStatusCode.BadRequest);
        }
    }

Thnaking you. Regards,Sekhar

Upvotes: 0

Views: 487

Answers (1)

laza
laza

Reputation: 91

The methods PUT and DELETE are not allowed by default. You have to allow them in your web applications web.config.

Upvotes: 1

Related Questions