Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MVC Web Api difference between HttpPost and HttpPut

I'm new to MVC Web Api.

I want to have two different methods.

PUT localhost/api/user - to modify a user

POST localhost/api/user - to add a user

So my ApiController looks like this:

    [HttpPost]
    public bool user(userDTO postdata)
    {
        return dal.addUser(postdata);
    }

    [HttpPut]
    public bool user(userDTO postdata)
    {
        return dal.editUser(postdata);
    }

However my HttpPut method says "already defines a member called user with the same parameter types.

Shouldn't the [HttpPost] and [HttpPut] make the methods unique?

like image 568
Lord Vermillion Avatar asked Apr 28 '16 06:04

Lord Vermillion


1 Answers

MVC Web Api difference between HttpPost and HttpPut

An HTTP PUT is supposed to accept the body of the request, and then store that at the resource identified by the URI.

An HTTP POST is more general. It is supposed to initiate an action on the server. That action could be to store the request body at the resource identified by the URI, or it could be a different URI, or it could be a different action.

PUT is like a file upload. A put to a URI affects exactly that URI. A POST to a URI could have any effect at all.

already defines a member called user with the same parameter types

You can't have multiple methods with the same signature within the same scope like that i.e. same return type and parameter type.

[HttpPost]
public bool User(userDTO postdata)
{
    return dal.addUser(postdata);
}

[HttpPut]
[ActionName("User")]
public bool UserPut(userDTO postdata)
{
    return dal.editUser(postdata);
}

more related ans. check this . GET and POST methods with the same Action name in the same Controller

like image 88
Nazmul Hasan Avatar answered Oct 11 '22 06:10

Nazmul Hasan