Web API - 405 - The requested resource does not support http method 'PUT'

前端 未结 10 697
说谎
说谎 2020-12-01 14:10

I have a Web API project and I am unable to enable \"PUT/Patch\" requests against it.

The response I get from fiddler is:


HTTP/1.1 405 Method Not Al         


        
10条回答
  •  旧时难觅i
    2020-12-01 14:56

    For me it was as many of the other posters had claimed. Chances are you have everything setup correctly in your webapiconfig, however you're just missing something silly.

    In my case I had a route defined as:

    [HttpPut]
        [Route("api/MilestonePut/{Milestone_ID}")]
        public void Put(int id, [FromBody]Milestone milestone)
        {
            db.Entry(milestone).State = System.Data.Entity.EntityState.Modified;
            db.SaveChanges();
        }
    

    See the problem? The parameter is defined as Milestone_ID in the route, but as id in the function itself. You would think .NET would be smart enough to realize this, but it definitely isn't and will not work.

    Once I changed it to match the parameter like so:

    [HttpPut]
        [Route("api/MilestonePut/{id}")]
        public void Put(int id, [FromBody]Milestone milestone)
        {
            db.Entry(milestone).State = System.Data.Entity.EntityState.Modified;
            db.SaveChanges();
        }
    

    everything worked like a charm.

提交回复
热议问题