简体   繁体   English

调用Web API删除方法时不允许HTTP错误405方法?

[英]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. 我正在尝试从MVC控制器应用程序调用到WEBAPI应用程序方法。 I am getting error 405 method not allowed. 我收到错误405方法不允许。 Its working fine while calling GET and POST. 调用GET和POST时工作正常。

MVC Applicaiton: MVC应用程序:

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

WEB API application: 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);
        }
    }

Thnaking you. 感动你 Regards,Sekhar 问候,Sekhar

The methods PUT and DELETE are not allowed by default. 默认情况下不允许使用PUT和DELETE方法。 You have to allow them in your web applications web.config. 您必须在您的Web应用程序web.config中允许它们。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM