tangokhi
tangokhi

Reputation: 1005

ASP.NET Web Api duplicate verbs

I have created a web api (REST) called Filter that has multiple get methods such as

GetCompany GetCustomers GetOrders

Is this correct practise or should I have different web api for different entities? Should I have same http verb (GET) duplicated in the same WEB API.

What about other verbs (POST or PUT)?

In another service we have one case where we want to update a specific field and another case where we can update anything except that specific field in the record. Should one method (POST or PUT) be used for both cases or can I have two separate methods?

I am calling these methods from angularjs $http service.

Upvotes: 2

Views: 221

Answers (1)

Alex
Alex

Reputation: 38499

You should have a different controller for each resource (entity)

Then a Get method on your CustomersController for example

Your urls would then be

/Company
/Customers
/Orders

etc...

Your HTTP verbs are then routed to the corresponding methods in those controllers. So, GET request to /Customers would be routed to your Get() method on that controller

Alternatively, if you really insist on one controller, you could use Attribute Routing, along with verb attributes

Something like

public class FilterController : ApiController
{
    [HttpGet]
    [Route("orders")]
    public IHttpActionResult GetOrders()
    { }


    [HttpGet]
    [Route("customers")]
    public IHttpActionResult GetCustomers()
    { }


    [HttpPut]
    [Route("customers")]
    public IHttpActionResult UpdateOrders()
    { }
}

But this will get pretty big, pretty quick, and I don't generally suggest doing it like this.

One controller per resource is much cleaner.

Upvotes: 4

Related Questions