ASP.NET Web Api: The requested resource does not support http method 'GET'

后端 未结 11 662
傲寒
傲寒 2020-12-04 18:50

I\'ve got the following action on an ApiController:

public string Something()
{
    return \"value\";
}

And I\'ve configured my routes as f

相关标签:
11条回答
  • Have the same Setup as OP. One controller with many actions... less "messy" :-)

    In my case i forgot the "[HttpGet]" when adding a new action.

    [HttpGet]
    public IEnumerable<string> TestApiCall()
    {
        return new string[] { "aa", "bb" };
    }
    
    0 讨论(0)
  • 2020-12-04 19:13

    All above information is correct, I'd also like to point out that the [AcceptVerbs()] annotation exists in both the System.Web.Mvc and System.Web.Http namespaces.

    You want to use the System.Web.Http if it's a Web API controller.

    0 讨论(0)
  • 2020-12-04 19:14

    I don't know if this can be related to the OP's post but I was missing the [HttpGet] annotation and that was what was causing the error, as stated by @dinesh_ravva methods are assumed to be HttpPost by default.

    0 讨论(0)
  • 2020-12-04 19:15

    Although this isn't an answer to the OP, I had the exact same error from a completely different root cause; so in case this helps anybody else...

    The problem for me was an incorrectly named method parameter which caused WebAPI to route the request unexpectedly. I have the following methods in my ProgrammesController:

    [HttpGet]
    public Programme GetProgrammeById(int id)
    {
        ...
    }
    
    [HttpDelete]
    public bool DeleteProgramme(int programmeId)
    {
        ...
    }
    

    DELETE requests to .../api/programmes/3 were not getting routed to DeleteProgramme as I expected, but to GetProgrammeById, because DeleteProgramme didn't have a parameter name of id. GetProgrammeById was then of course rejecting the DELETE as it is marked as only accepting GETs.

    So the fix was simple:

    [HttpDelete]
    public bool DeleteProgramme(int id)
    {
        ...
    }
    

    And all is well. Silly mistake really but hard to debug.

    0 讨论(0)
  • 2020-12-04 19:18

    My issue was as simple as having a null reference that didn't show up in the returned message, I had to debug my API to see it.

    0 讨论(0)
  • 2020-12-04 19:20

    If you have not configured any HttpMethod on your action in controller, it is assumed to be only HttpPost in RC. In Beta, it is assumed to support all methods - GET, PUT, POST and Delete. This is a small change from beta to RC. You could easily decore more than one httpmethod on your action with [AcceptVerbs("GET", "POST")].

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