.Net Web API is an easy way to implement a RESTful web service using all of the goodness that the .net framework provides. Once you understand the basic principles of REST, then a .net Web API will be very easy to implement.
Web API is built on .net’s modular, pluggable pipeline model. This means that when a server hosting a web API receives a request, it passes through .nets request pipeline first. This enables you to easily add your own modules if you find that the default capabilities are not enough for your needs. With the recent announcements on ASP.net vNext, this also means you can potentially host your Web API outside of Windows Server which opens up a whole range of use cases. See http://www.asp.net/vnext for detail.
Web API uses the Controller and Action concepts from MVC so if you already understand.net MVC you are in a good place. If you don’t, then Web API is a great way to learn MVC.
Resources are mapped directly to controllers; you would typically have a different controller for each of your main data entities (Product, Person, Order etc). Web API uses the .net routing engine to map URLs to controllers. Typically, APIs are held within a ‘/api/’ route which helps to distinguish API controllers from other non-API in the same website.
Actions are used to map to specific HTTP verbs, for example, you would typically have a GET action which returns all of the entities. This action would respond to /api/Products (where ‘products’ is your controller) and would look something like this:
public IEnumerable<string> Get()
{
return new string[] { "value1", "value2" };
}
{
return new string[] { "value1", "value2" };
}
You may also have a GET action which accepts a specific ID and returns a specific entity. It would respond to /api/Products/81 and would look something like this:
public string Get(int id)
{
return "value";
}
{
return "value";
}
There are many great hidden benefits to using Web API which you may not realize but actually save you a lot of work.
Web API is part of the ‘One ASP.net’ family which means that it natively supports all of the great shared features you may currently use with MVC or web forms, this includes (these are just a few examples):
- · Entity Framework
- · Authorisation and identity
- · Scaffolding
- · Routing
No comments:
Post a Comment