Anil Purswani
Anil Purswani

Reputation: 1887

How to add multiple Get Post and Delete methods in RESTful Service

I am new to REST Services and would like to know how we can add multiple Get / Post / Delete methods.

for e.g. We are having following Get Methods: -

GetAllUsers()
GetUserByID(int id)
GetUserByName(string name)

Similarly, Delete methods: -

DeleteAllUsers()
DeleteUserByID(int id)
DeleteUserByName(string name)

Post/Put Methods: -

PutCreateDefaultUser()
PutCreateUser(User user)
PutCreateMultipleUsers(User[] users)

So how to define Get/Delete/Post/Put methods in above case. Is that name it self says which is get / delete /put / post

Also How to set the uri template for each?

What will be the URI of each method?

Note: I am using MVC4 .Net Web API project, I am NOT using WCF

Upvotes: 3

Views: 5746

Answers (2)

jpgrassi
jpgrassi

Reputation: 5732

Your examples point out to more of an RPC implementation. REST is based on resources. Each resource has its methods. to Get, Update, Insert and Delete. If you are planning to have what you said in your question, you can do it in your ASP.NET API with no problem: (But be sure that this is NOT REST)

Update (2018) After some time and experience (and after a user comment on this old answer) I realized it was wrong to say that OP endpoints were not Restfull. The routes can be easily done to achieve that, as my examples already have shown. Funny how we learn and change our own ideas/opinions with time. :)

UserController

[RoutePrefix("api/v1")]
public class UserController : ApiController
{

    [HttpGet]
    [Route("users")]
    public HttpResponseMessage GetAllUsers()
    {
        ...
    }

    [HttpGet]
    [Route("users/{id:int}")]
    public HttpResponseMessage GetUserByID(int id)
    {
        ...
    }

    [HttpGet]
    [Route("users/{name:string}")]
    public HttpResponseMessage GetUserByName(string name)
    {
        ...
    }

    [HttpDelete]
    public HttpResponseMessage DeleteAllUsers()
    {
        ...
    }

    [HttpDelete]
    [Route("users/{id:int}")]
    public HttpResponseMessage DeleteUserByID(int id)
    {
        ...
    }
}

With the HttpAttributes, you can have as many HttpDeletes you want. Just put the attribute on top of the action and you're good to go. It also enforces that the methods can only be called using that HTTP verb. So in the Delete above, if you do a call with a GET verb, you'll get nothing. (The action will not be found)

You can also explicitly give a custom route to your action if you so desire. For instance, your call to GetUserByID would be:

GET: http://localhost:2020/api/v1/users/1

Upvotes: 4

Nick
Nick

Reputation: 6588

Most of the information you require can be found here:

You can specify the HTTP method with an attribute: AcceptVerbs, HttpDelete, HttpGet, HttpHead, HttpOptions, HttpPatch, HttpPost, HttpPut. Otherwise, if the name of the controller method starts with "Get", "Post", "Put", "Delete", "Head", "Options", or "Patch", then by convention the action supports that HTTP method. If none of the above, the method supports POST.

The Uri will depend on the name of the controller: /api/controller-name/GetAllUsers

Upvotes: 1

Related Questions