2

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.

2
  • 2
    Do you not have different controllers such as CompanyController, CustomerController and OrderController? Or am I missing something? Commented Nov 20, 2015 at 16:29
  • 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. Commented Nov 20, 2015 at 16:52

1 Answer 1

4

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.

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

4 Comments

Please see my comments above. For my other problem which is about updating a resource, I have one controller. According to what you are suggesting, I should have just one method for PUT and in that method I should have some logic where I need to identify whether to run one logic path or another? I see your point that doing something like that will be pure but won't that method will be doing too much where as if I have two methods to represent different update scenarios it is more concise. What are the cons of having two put methods rather than one on the same controller.
the cons are readability. If you have one controller per resource, navigating your code is much easier. If you want to change something to do with updating customers, you know to go to the CustomersController... makes for much greater readability.
Thanks Alex. I did exactly (i.e FilterController above) that but I wasn't sure whether it is the right thing to do or not and you have already answered that. I guess for my simple case I will go with one controller approach knowing that it is not very clean and may be not restful as well.
it can still be restful, just use the attribute routing approach :)

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.