Blog

How to Create a RESTful application using MVC Rest library

By Fernando G.

Overview

First off, we need to have a quick overview of what REST means to the development community, and we can start by what does REST stands for?  It stands for Representational State Transfer.  Right, it did not help a lot, right?

In the SOA era, we have Service Oriented Systems interacting between them exposing Services, some applications act as consumers and some as producers.  At the end, a Service Oriented Architecture allows an organization to create a well thought and organized set of libraries being used uniformly among all of the participants with no redundancy of business rules and specially having a single set of Services.  Well, not always the case.  It results extremely painful when the Services are done in different technologies; Interoperability between the different implementations is the major issue among Service consumers and producers.  Another problem is that development teams do not take in considerations to other areas of the company.   This ends up with duplicate Services because they do not provide what the potential consumers need, it is too painful to reuse.

Well, REST style architecture is one alternative to SOA.  REST is all about identifying Resources and not Services.  These Resources are Stateless, Cacheable and in consequence are stable, scalable, with high performance and simple.

Asp.Net MVC

Microsoft saw a huge potential to MVC pattern for web development.  They already tried with a library called Microsoft User Interface Process (UIP) implementation back in 2003, but it was not very popular among developers so it lost track and its last update was version 2 in 2004.

MVC is very REST oriented and already implements Request providers such as JsonRequest.  Well, there is a library that goes beyond what ASP.NET MVC already provides, and it is a set of classes that give us a transparent way of exposing Resources with different formats including XML, Json and most importantly it allows us to keep an Html view.

How to create a new MVC Rest application

First you need to create an ASP.NET MVC Web Application, you have to specify the Name and Location of it, and then click Ok.

RESTful-application-MVC-1

 

No need to create a Test project for now.

RESTful-application-MVC-2

You may attach your preferred database to your solution; I am including the NerdDinner database for simplicity.  Then, I will create use LINQ to SQL to create the Model.  For me, the Resource is Dinner.

RESTful-application-MVC-3

Add the Controller.

RESTful-application-MVC

 

Add the Reference to the MVC Rest dll named “System.Web.Mvc.Resources.dll”.  You can download the source code from CodePlex website and compile it using Visual Studio 2008.

RESTful-application-MVC-1

Add a simple list View of the Dinner Resource.

RESTful-application-MVC-6

Write the Controller Index method:

RESTful-application-MVC-1

 

Run the application and go to /Dinner, and the next should appear:

RESTful-application-MVC-1

 

Then, add the special attribute to enable you controller as a RESTful application.

RESTful-application-MVC-1

 

Hope this information is useful.

 

 

Comments

Leave a comment

 
 
 
 
CAPTCHA Image Validation