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?

4
  • 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. Commented Apr 28, 2016 at 6:34
  • 1
    Refer to here: stackoverflow.com/questions/9552761/… for a potential solution Commented Apr 28, 2016 at 6: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); } Commented Apr 28, 2016 at 6:39
  • Related post - What is the difference between POST and PUT in HTTP? Commented Jan 28, 2022 at 6:30

2 Answers 2

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

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

Comments

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

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.