17
votes

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.

5
Which webserver are you using? Because in IIS you have to manually enable Put and Delete see forums.iis.net/t/1166025.aspxnemesv
IIS Express that comes with VS 2012 Express for Webuser1166905
Then see the How do I enable verbs like PUT/DELETE for my web application? from the IIS Express FAQ.nemesv
Which verbs do I change on the host file though, there's quite a fewuser1166905
or one might disable WebDAV publishing from Control PanelAmit Kumar Ghosh

5 Answers

23
votes

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

5
votes

Do you have [HttpPut] attribute on your UpdateClient action? Also, do you have a route that takes in the {action} as the routeTemplate? For example:

config.Routes.MapHttpRoute(
    name: "DefaultApi",
    routeTemplate: "api/{controller}/{action}/{id}",
    defaults: new { id = RouteParameter.Optional }
);

One more thing, try 'application/json' for the content-type in your ajax code instead of 'json'.

2
votes

Looks like these two lines were wrong, I changed them as follows:

contentType: 'application/json',
data: "{client: " + ko.toJSON(model.selectedClient()) + "}",

And now goes in.

1
votes

Note to future troubleshooters: I got this error when my "Put" controller was unintentionally expecting an additional parameter that wasn't being used.

0
votes

My issue was that under project properties, I was using Local IIS instead of IIS Express, and it defaulted to port 80 which blocked DELETE requests. Changing to IIS Express fixed it.