405 method not allowed Web API

前端 未结 21 1467
栀梦
栀梦 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 19:00

    I was having exactly the same problem. I looked for two hours what was wrong with no luck until I realize my POST method was private instead of public .

    Funny now seeing that error message is kind of generic. Hope it helps!

    0 讨论(0)
  • 2020-11-27 19:02

    We had a similar issue. We were trying to GET from:

    [RoutePrefix("api/car")]
    public class CarController: ApiController{
    
        [HTTPGet]
        [Route("")]
        public virtual async Task<ActionResult> GetAll(){
    
        }
    
    }
    

    So we would .GET("/api/car") and this would throw a 405 error.


    The Fix:

    The CarController.cs file was in the directory /api/car so when we were requesting this api endpoint, IIS would send back an error because it looked like we were trying to access a virtual directory that we were not allowed to.

    Option 1: change / rename the directory the controller is in
    Option 2: change the route prefix to something that doesn't match the virtual directory.

    0 讨论(0)
  • 2020-11-27 19:02

    In my case I had a physical folder in the project with the same name as the WebAPI route (ex. sandbox) and only the POST request was intercepted by the static files handler in IIS (obviously).

    Getting a misleading 405 error instead of the more expected 404, was the reason it took me long to troubleshoot.

    Not easy to fall-into this, but possible. Hope it helps someone.

    0 讨论(0)
提交回复
热议问题