Web API or MVC Controller

It’s quite difficult to make decision but definitely not a rocket science. Following points can be considered:

  • IMO, MVC controller is aiming to expose to specific site where API can be used by many client application. So first and foremost step to verify that X functionality is utilize only by one or number of client. For example, finding the price of product is one in shopping site and Second Marketing Dashboard apps to compare product price with other vendor. Both are different application, but are good to create Product Price API Service.
  • As we know the output of MVC controller is either HTML markup or JSON formatted Data. If you need multiple data formats such as XML and JSON than API allows an easy configuration. Because WebApi decides the data format automatically based on the Accept header. But developer requires to explicitly specifying the data format (ActionResult or JsonResult) while writing the action methods.
  • Let ask yourself, your application need such service which is able to determine what data format to use for sending to client? If yes, I am sure you are looking Content Negotiation. CN is a process by which Web API inspects the incoming request and HTTP headers accompanying the request to figure out what response format(s) the client can understand. Based on this checking Web API sends the output. The Two HTTP headers play important role in content negotiation are: 1) Accept 2) Content-Type. No Doubt its really nice feature for an API framework, although this is not much used.
  • The exposing service through controller could be good choice but is highly depend on IIS Hosting. This is added advantage for API, where you can do self hosting.

Please share your thoughts.