2
votes

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.

1
Do you not have different controllers such as CompanyController, CustomerController and OrderController? Or am I missing something?Win
I have one controller called FilterController. The reason I created one was that I know I will ever need a get method and creating multiple controllers which just contains one method each seems too much for too little.tangokhi

1 Answers

4
votes

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.