17

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
  • 1
    Which webserver are you using? Because in IIS you have to manually enable Put and Delete see forums.iis.net/t/1166025.aspx Commented Oct 9, 2012 at 19:55
  • IIS Express that comes with VS 2012 Express for Web Commented Oct 9, 2012 at 20:09
  • Then see the How do I enable verbs like PUT/DELETE for my web application? from the IIS Express FAQ. Commented Oct 9, 2012 at 20:13
  • Which verbs do I change on the host file though, there's quite a few Commented Oct 9, 2012 at 20:24
  • or one might disable WebDAV publishing from Control Panel Commented Jul 8, 2015 at 10:43

5 Answers 5

23

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

Sign up to request clarification or add additional context in comments.

4 Comments

+1, It solved my problem, but what if we want to have that module also?
Try adding the following in your web.config file code<system.webServer> <modules runAllManagedModulesForAllRequests="true"> <remove name="WebDAVModule" /> </modules> </system.webServer>
In my case I also had to add <handlers><remove name="WebDAV" /></handlers> to the system.webServer section (I'm on IIS8).
or one might disable WebDAV publishing from Control Panel.
5

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'.

Comments

2

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.

Comments

1

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

Comments

0

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.