Using API in MVC applications

5.00 avg. rating (97% score) - 1 vote

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

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. 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. 🙂
  2. We add a model class as Reservation.cs into the Models folder. The Code snippet goes below :
  3. Now we add an interface IReservationRepository.cs into the models folder again.The code snippet goes below:
  4. 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.)
  5. 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

  6. 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.