Error
Error

Reputation: 825

HTTP 405 invalid method (HTTP verb) when using $http.post

I've come to some strange behaviour using web api. I'm using attribute routing, and making post on server. Controller:

    [Route("names")]
    public ResultDTO SaveData(SomeDTO dto)
    {
       //somecode
        ...
    } 

and i'm calling it

     $http.post('/api/mycontroller/names', $scope.model.dto).success(function 
      (data) { ...

It's working. However, if I rename my method

    [Route("names")]
    public ResultDTO GetData(SomeDTO dto)
    {
       //somecode
        ...
    } 

it's not working and I get HTTP 405 error The page you are looking for cannot be displayed because an invalid method (HTTP verb) was used to attempt access,

However it's working if I change calling from $http.post to $http.get

Obviously, I won't name my method starting with GetSomeMethod if I'm posting data, but I'm curious, shouldn't defined route

   [Route("names")]

work with $http.post, no matter how I actually call method that will handle that post? More specific, why $http.post won't work if I named my method GetSomething, but it will if I change method name to, for example, GotSomething or SaveSomething?

Upvotes: 0

Views: 3932

Answers (2)

Kundan Rai
Kundan Rai

Reputation: 75

Use proper verbs for $http.post(***) - [HttpPost] and for $http.get(***) - [HttpGet]

Upvotes: 0

Roman Marusyk
Roman Marusyk

Reputation: 24569

Try to add route attribute

[HttpPost]

and then you can name your action as you wish.

Web API looks at the HTTP method, and then looks for an action whose name begins with that HTTP method name. For example, with a GET request, Web API looks for an action that starts with Get..., such as GetContact or GetAllContacts. This convention applies only to GET, POST, PUT, and DELETE methods.

See more here

Upvotes: 4

Related Questions