9

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?

its4zahoor
  • 1,709
  • 1
  • 16
  • 23
Lord Vermillion
  • 5,264
  • 20
  • 69
  • 109
  • The restriction is a fundamental part of C#, you can't have two methods with the same name and arguments. Attributes have no say on that, neither does MVC. – Rob Apr 28 '16 at 06:34
  • 1
    Refer to here: http://stackoverflow.com/questions/9552761/get-and-post-methods-with-the-same-action-name-in-the-same-controller for a potential solution – Rob Apr 28 '16 at 06:35
  • decorate your action method with both the types. eg [HttpPost] in the next line [HttpPut] public bool user(userDTO postdata) { return dal.addUser(postdata); } – Karthik M R Apr 28 '16 at 06:39
  • Related post - [What is the difference between POST and PUT in HTTP?](https://stackoverflow.com/q/630453/465053) – RBT Jan 28 '22 at 06:30

2 Answers2

11

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

devklick
  • 2,000
  • 3
  • 30
  • 47
Nazmul Hasan
  • 10,130
  • 7
  • 50
  • 73
8

No attribute can make your methods unique when you have 2 methods with the same name and the same signature.

The solution in your case would look something like this.

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

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

P.S: The convention for naming methods is that you should use PascalCase and use verbs when naming your methods.

Method Naming Guidelines

Kerim Emurla
  • 1,141
  • 8
  • 15