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

后端 未结 11 738
傲寒
傲寒 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条回答
  •  孤城傲影
    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.

提交回复
热议问题