HTTP错误405调用web api删除方法时不允许的方法?

时间:2015-04-14 16:18:34

标签: c# asp.net-mvc asp.net-mvc-4 asp.net-web-api

我试图从MVC控制器应用程序调用WEBAPI应用程序方法。我得到错误405方法不允许。它在调用GET和POST时工作正常。

MVC Applicaiton:

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

WEB API应用程序:

[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);
        }
    }

偷偷摸摸你。   的问候,Sekhar

1 个答案:

答案 0 :(得分:1)

默认情况下不允许使用PUT和DELETE方法。您必须在Web应用程序web.config中允许它们。