405 method not allowed Web API

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

    You can also get the 405 error if say your method is expecting a parameter and you are not passing it.

    This does NOT work ( 405 error)

    HTML View/Javascript

    $.ajax({
             url: '/api/News',
             //.....
    

    Web Api:

    public HttpResponseMessage GetNews(int id)
    

    Thus if the method signature is like the above then you must do:

    HTML View/Javascript

    $.ajax({
             url: '/api/News/5',
             //.....
    
    0 讨论(0)
  • 2020-11-27 18:44

    If you have a route like

    [Route("nuclearreactors/{reactorId}")]
    

    You need to use the exact same parameter name in the method e.g.

    public ReactorModel GetReactor(reactorId)
    {
     ...
    }
    

    If you do not pass the exact same parameter you may get the error "405 method not allowed" because the route will not match the request and WebApi will hit a different controller method with different allowed HTTP method.

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

    This does not answer your specific question, but when I had the same problem I ended up here and I figured that more people might do the same.

    The problem I had was that I had indeliberately declared my Get method as static. I missed this an entire forenoon, and it caused no warnings from attributes or similar.

    Incorrect:

    public class EchoController : ApiController
    {
        public static string Get()
        {
            return string.Empty;
        }
    }
    

    Correct:

    public class EchoController : ApiController
    {
        public string Get()
        {
            return string.Empty;
        }
    }
    
    0 讨论(0)
  • 2020-11-27 18:49

    This error can also occur when you try to connect to http while the server is on https.

    It was a bit confusing because my get-requests were OK, the problem was only present with post-requests.

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

    I was getting the 405 on my GET call, and the problem turned out that I named the parameter in the GET server-side method Get(int formId), and I needed to change the route, or rename it Get(int id).

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

    Here is one solution

    <handlers accessPolicy="Read, Script"> <remove name="WebDAV" /> </handlers>

    docs.microsoft.com solution article

    and remove WebDAV from modules

    <remove name="WebDAVModule" />

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