405 method not allowed Web API

前端 未结 21 1649
栀梦
栀梦 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条回答
  •  慢半拍i
    慢半拍i (楼主)
    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 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.

提交回复
热议问题