I am designing a set of API that do not require the typical CRUD functionality. I only need to serve data to clients and this involves applying various filters to the data fetched from my backend database. For instance, currently I have the following API:
Service.asmx/GetEvents
which has the following signature:
public CustomObject GetEvents(string Location = "", string Company = "", string PersonType = "", string FromDate = "", string ToDate = "")
{
....
}
If all my API look somewhat like this, how am I to use the MVC4 Web API? I am currently using AVC4 for the rest of my project and Web Services just to expose the API and I am sure this is a wrong way of doing things. I am happy with my current service but I did see several posts scattered around SO and the blogosphere that Web API is the way to go because that is the future. Any suggestions?
You write a controller that derives from ApiController and expose an action. You could also encapsulate your filter logic into a model:
and the controller:
Now assuming default routes, your API is ready for consumption at
/api/events.If you want to learn more about the Web API I suggest you starting on the official site: http://www.asp.net/web-api