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

前端 未结 10 507
说谎
说谎 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条回答
  • 2020-12-01 14:50

    This is also the error message returned if you forget to make the Put() method on your API controller public. Which is obvious in hindsight, but caused me a good ten minutes of head-scratching.

    0 讨论(0)
  • 2020-12-01 14:51

    You should configure it in your webservers config. It depends on the type of the webserver, where your can do it. For example by IIS, you can use a web.config file to do that in your document root. By cross-origin requests you have to add CORS headers to the response, to allow origins, methods, etc...

    note: Probably you can do something about this with the ASP.NET framework as well, but I think it's unlike.

    0 讨论(0)
  • 2020-12-01 14:52

    Maybe it's late now, but someone can use this.

    I wanted to use PUT request and I just sent stringified object to web api and in put method only accepted that object.

    JQUERY

    let musterija = {
                Name: name,
                Email: email,
                Password: password,
                Username: logUser.Username,
                Lastname: lastname,
                GenderString: gender,
                Jmbg: identification,
                PhoneNumber: phone,
            };
    
            $.ajax({
                method: "PUT",
                url: "/api/Musterija",
                data: JSON.stringify(musterija),
                contentType: "application/json; charset=utf-8",
                dataType: "json",
                success: function () {
                    alert("Entity updated");
                    EmptyAllInputs();
                    $('#divprofile').show();
                    $('#divupdate').hide();
                },
                error: function (msg) {
                    alert("Fail - " + msg.responseText);
                }
            });
    

    WEB API

        [HttpPut]
        public HttpResponseMessage PutMusterija(Musterija m)
    
    0 讨论(0)
  • 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.

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