0

I'm getting a 405 Method not found error when trying to use a $http.put in angular to my Web api controller.

I've tried about everything I can find out there, mostly having to do with WebDav. I have another Web api project I've done and never ran into this issue. Below is my sample call within angular. It's frusterating, I have everything basically the same in web config of another Web api project that seems to work fine.

return $http.put("/api/Account/PasswordChange",{params:{"currentPass":currentPass,"newPass":newPass,"confirmPass":confirmPass}})
                        .then(function (response) {
                            return response.data;
                        },
                            function (response) {

                                return $q.reject(response);
                            });

Here is my web api routing

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

and here is web api controller being called

public IHttpActionResult PutPasswordChange(string currentPass,string newPass,string confirmPass)
    {
        return Ok();
    }
Dylan
  • 1,068
  • 12
  • 25
  • Have you set up a breakpoint in your route to see if it gets hit? – Maria Ines Parnisari Nov 04 '16 at 16:22
  • Nothing gets hit, all i get back is 405 method not found. If I do a post to the same controller, different function, it works fine.... Its only http puts and deletes. – Dylan Nov 04 '16 at 16:23
  • Well the route needs to be configured to respond to PUT requests. Can you edit your post to include your backend code? – Maria Ines Parnisari Nov 04 '16 at 16:23
  • Ok.I have added it. – Dylan Nov 04 '16 at 16:27
  • If your controller is defined as `class MyController` then the URL should use just `/My/...`, not `/MyController/...`. – Peter B Nov 04 '16 at 16:33
  • Its just an example. I have other things working fine. My controller is actually AccountController, and I just put "Acccount". The problem isnt the path, its the fact that I can do everything just fine except put and delete. – Dylan Nov 04 '16 at 16:34

2 Answers2

0

Change the URL you're calling to "/api/Account" instead.

You don't need "Controller" or "PasswordChange". It should just map the controller name (without the word Controller) to the PutPasswordChange method automatically based on the word "Put" being in the method name, and the HTTP method you've used.

You could even rename the controller method to simply "Put" and it would work.

N.B. Depending on your browser and server combo you may need to do some stuff around enabling OPTIONS requests as well when dealing with PUT and DELETE, but that's a whole separate topic.

ADyson
  • 57,178
  • 14
  • 51
  • 63
  • Those were just examples. I know how to call a controller properly. Its something else that is breaking it as everything except put and delete work fine. I've never had to configure anything to get this working before. Which is why im confused why its not working now. – Dylan Nov 04 '16 at 16:39
  • @Dylan well have you actually tried changing the URL structure to double check it's not the issue? That's how I've always done it and I've got plenty of working examples using the convention I've just described. I can't personally see anything else the matter with the code, in so far as what you've posted. – ADyson Nov 04 '16 at 16:40
  • @Dylan also are you certain it's the PUT call which is giving the 405, and not a preceding OPTIONS call? And have you tried it in any different server environments? – ADyson Nov 04 '16 at 16:43
  • I've tried it your way, getting the same thing. "The requested resource does not support http method 'PUT'." – Dylan Nov 04 '16 at 16:44
  • @Dylan what about my second comment? Have you ruled out those potential issues? – ADyson Nov 04 '16 at 16:58
  • there's also a ton of good advice here: http://stackoverflow.com/questions/23502198/web-api-405-the-requested-resource-does-not-support-http-method-put if you haven't seen them already – ADyson Nov 04 '16 at 16:59
0

Please see http://www.ryadel.com/en/error-405-methods-not-allowed-asp-net-core-put-delete-requests/ I had to add on publishd site web.config:

<modules runAllManagedModulesForAllRequests="false">
    <remove name="WebDAVModule" />
</modules>
Manuel Alves
  • 3,885
  • 2
  • 30
  • 24