405 method not allowed Web API

前端 未结 21 1571
栀梦
栀梦 2020-11-27 18:33

This error is very common, and I tried all of the solutions and non of them worked. I have disabled WebDAV publishing in control panel and added this to my web config file:<

21条回答
  •  轻奢々
    轻奢々 (楼主)
    2020-11-27 18:39

    I could NOT solve this. I had CORS enabled and working as long as the POST returned void (ASP.NET 4.0 - WEBAPI 1). When I tried to return a HttpResponseMessage, I started getting the HTTP 405 response.

    Based on Llad's response above, I took a look at my own references.

    I had the attribute [System.Web.Mvc.HttpPost] listed above my POST method.

    I changed this to use:

    [System.Web.Http.HttpPostAttribute]
    [HttpOptions]
    public HttpResponseMessage Post(object json)        
    {
        ...
        return new HttpResponseMessage { StatusCode = HttpStatusCode.OK };
    }
    

    This fixed my woes. I hope this helps someone else.

    For the sake of completeness, I had the following in my web.config:

    
        
            
            
            
            
            
            
        
    
    

提交回复
热议问题