Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

405 Message, method not allowed with Web Api

I have got the following on an API controller:

public void UpdateClient(Client client)
    {
        try
        {
            if (ModelState.IsValid)
            {
                db.Entry(client).State = EntityState.Modified;
                db.SaveChanges();
            }
        }
        catch
        {
            throw new HttpResponseException(new HttpResponseMessage(HttpStatusCode.NotFound));
        }
    }

And the following on the page:

$.ajax({
            url: "api/client/UpdateClient",
            type: "PUT",
            contentType: 'json',
            data: ko.toJSON(model.selectedClient()),
            success: function (result) {
                getClients();
                $("#loader").hide();
            },
            failure: function (result) {
                alert(result.d);
                $("#loader").hide();
            },
            error: function (XMLHttpRequest, textStatus, errorThrown) {
                alert("An error occurred, please try again.");
                $("#loader").hide();
            }
        });

But this gives the error 405 Method Not Allowed, can anyone see where I may have gone wrong? For reference the url for the api is ok as I use the same api controller for other functions too.

Also the selectedClient() is a Client object received via WebApi so should match perfectly to PUT up again.

like image 696
user1166905 Avatar asked Oct 09 '12 19:10

user1166905


People also ask

What is a 405 Method not allowed?

The 405 Method Not Allowed error is an HTTP response status that tells you that a web browser has made a request to access one of your website's pages. Your web server has received the request and recognized it but has rejected the specific HTTP method that was used.

What is Method not allowed in Postman?

However, in general, following HTTP standards, a 405 response status code means “Method Not Allowed”. So literally, a POST method is not allowed for that url endpoint on the server, in question. The server explicitly denies a POST method to that endpoint.


1 Answers

If you are using IIS7 and have WebDav installed, try removing it. I was getting the same error only with the PUT verb and it solved the issue

Update: You can read about WebDav here: http://www.iis.net/learn/get-started/whats-new-in-iis-7/what39s-new-for-webdav-and-iis-7

like image 108
Salman Avatar answered Sep 22 '22 06:09

Salman