Legend
Legend

Reputation: 116820

For my scenario, I don't understand how I can leverage Web API

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?

Upvotes: 0

Views: 83

Answers (1)

Darin Dimitrov
Darin Dimitrov

Reputation: 1038820

You write a controller that derives from ApiController and expose an action. You could also encapsulate your filter logic into a model:

public class Filter
{
    public string Location { get; set; }
    public string Company { get; set; }
    public string PersonType { get; set; }
    public string FromDate { get; set; }
    public string ToDate { get; set; }
}

and the controller:

public EventsController: ApiController
{
    public CustomObject Get(Filter filter)
    {
        ...
    }
}

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

Upvotes: 1

Related Questions