Introduction
The Web Api (Application Programming Interface
) feature is based on a special kind of controller to an MVC framework application, its normally as we do in MVC :). An api controller distinguishes itself from the normal controllers by the following :-
- Action methods return model, rather than an ActionResult in MVC, objects.
- Action methods are selected based on the HTTP method used in the request.(we will elaborate on this a bit ahead as we go ) 🙂
The model objects that are returned from an API controller action method are encoded as JSON/XML
based on the browsers used and sent to the client.
Background
We work using normal MVC4 framework, choosing an empty template. There is also a Web Api template but we cannot get to know the steps using that. Lets get into deeper following the steps in the Code
section.
Using the code
A Web Api application is just a regular MVC framework app with the addition of a special kind of controller. Lets start with a small Demo for better understanding. Lets go step by step :-
Creating the Model & Repository
1 2 3 4 5 6 7 |
public interface IReservationRepository{ IEnumerable<Reservation> GetAll(); Reservation Get(int Id); Reservation Add(Reservation item); void Delete(int id); bool Update(Reservation item); } |
The GetAll
() method is of Enumerable type of Reservation as it expects a list of items as output. The Get
(Id) method is of type Reservation that expects an item that matches the Id passed/specified. The Add
(item) method is also a Reservation type as it adds a new item with the properties of the Reservation entity. The Delete
method simply deletes/remove the item with Id passed. The Update
method is boolean type as if model state is valid and update is successful its returns true.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 |
public class ReservationRepository : IReservationRepository { private List<Reservation> data = new List<reservation> { new Reservation {ReservationId = 1, Name = "ABC", Location = "London"}, new Reservation {ReservationId = 2, Name = "XYZ", Location = "US"}, new Reservation {ReservationId = 3, Name = "MNC", Location = "Paris"} }; private static ReservationRepository repo = new ReservationRepository(); public static IReservationRepository getRepository(){ return repo; } public IEnumerable GetAll(){ return data; } public Reservation Get(int Id){ var matchedItem = data.where(x => x.ReservationId == Id); return matchedItem.FirstOrDefault(); } public Reservation Add(Reservation item){ item.ReservationId = data.Count() + 1; data.Add(item); return item; } public void Delete(int is){ Reservation item = Get(id); if(item != null){ data.Delete(item); } } public bool Update (Reservation item){ Reservation Item = Get(item.ReservationId); if(Item != null){ Item.Name = item.Name; Item.Location = item.Location; return true; } else{ return false; } } } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
public class ReservationController:ApiController{ IReservationRepository repo = ReservationRepository.getRepository(); public IEnumerable<Reservation> GetAllReservation() { return repo.GetAll(); } public Reservation GetReservation(int id) { return repo.Get(id); } public Reservation PostReservation(Reservation item) { return repo.Add(item); } public bool PutReservation(Reservation item) { return repo.Update(item); } public void deleteReservation(int id) { repo.Delete(id); } } |
- Lets start creating an Empty template MVC application and name it ReservationDemo. Since we have empty MVC project then we only get Models, Controllers & Views as empty folder. 🙂
- We add a model class as
Reservation.cs
into the Models folder. The Code snippet goes below :
1234567891011public class Reservation{//Added the properties to the Reservation entitypublic int ReservationId {get; set;} // Uniquely identifies each model object.public string Name {get; set;}public string Location {get; set;}} - Now we add an interface IReservationRepository.cs into the models folder again.The code snippet goes below:
- Now lets add a Repository class to the Models itself that implements the interface IReservationRepository. The code snippet goes below :- (Remember we are adding here into the models folder.)
- The Home Controller a normal controller is created to return the main view page to our app. We just add a view to its Index()ActionResult method.
Creating API Controller
- Add a controller, right click on the contollers folder in the SolutionExplorer and select Add Controller from the popup menu. Change the controller name as ReservationController and select Empty Api Controller from the Template drop-down menu. The Code snippet goes as below:-
The game after this is with the Jquery where the data results are bind on the ajax calls.
Points of Interest
This is an interesting concept, using Api that returns JSON results that can be used to bind data in the viewpage easily, using knockoutJs, BackboneJs.
Referenced from Adam Freeman, Asp.Net MVC4 book.
History
Will post soon the data-bind using Knockout/Backbone Js. Hope this helps beginners like me.