405 method not allowed Web API

前端 未结 21 1468
栀梦
栀梦 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:51

    Chrome often times tries to do an OPTIONS call before doing a post. It does this to make sure the CORS headers are in order. It can be problematic if you are not handling the OPTIONS call in your API controller.

    public void Options() { }
    
    0 讨论(0)
  • 2020-11-27 18:55

    I tried many thing to get DELETE method work (I was getting 405 method not allowed web api) , and finally I added [Route("api/scan/{id}")] to my controller and was work fine. hope this post help some one.

         // DELETE api/Scan/5
        [Route("api/scan/{id}")]
        [ResponseType(typeof(Scan))]
        public IHttpActionResult DeleteScan(int id)
        {
            Scan scan = db.Scans.Find(id);
            if (scan == null)
            {
                return NotFound();
            }
    
            db.Scans.Remove(scan);
            db.SaveChanges();
    
            return Ok(scan);
        }
    
    0 讨论(0)
  • 2020-11-27 18:55

    I'm late to this party but as nothing above was either viable or working in most cases, here is how this was finally resolved for me.

    On the server the site/service was hosted on, a feature was required! HTTP ACTIVATION!!!

    Server Manager > Manage > Add Roles and Features > next next next till you get to Features > Under .NET (each version) tick HTTP Activation. Also note there is one hidden under >net > WCF Services.

    This then worked instantly! That was melting my brain

    0 讨论(0)
  • 2020-11-27 18:56

    I had the same exception. My problem was that I had used:

    using System.Web.Mvc; // Wrong namespace for HttpGet attribute !!!!!!!!!
    [HttpGet]
    public string Blah()
    {
        return "blah";
    }
    

    SHOULD BE

    using System.Web.Http; // Correct namespace for HttpGet attribute !!!!!!!!!
    [HttpGet]
    public string Blah()
    {
        return "blah";
    }
    
    0 讨论(0)
  • 2020-11-27 18:56

    For my part my POST handler was of this form:

    [HttpPost("{routeParam}")]
    public async Task<ActionResult> PostActuality ([FromRoute] int routeParam, [FromBody] PostData data)
    

    I figured out that I had to swap the arguments, that is to say the body data first then the route parameter, as this:

    [HttpPost("{routeParam}")]
    public async Task<ActionResult> PostActuality ([FromBody] PostData data, [FromRoute] int routeParam)
    
    0 讨论(0)
  • 2020-11-27 19:00

    Another possible issue which causes the same behavior is the default parameters in the routing. In my case the controller was located and instantiated correctly, but the POST was blocked because of default Get action specified:

    config.Routes.MapHttpRoute(
        name: "GetAllRoute",
        routeTemplate: "api/{controller}.{ext}"/*,
        defaults: new { action = "Get" }*/ // this was causing the issue
    );
    
    0 讨论(0)
提交回复
热议问题