Restful urls in asp.net mvc with simple steps

. Monday, August 22, 2011
  • Agregar a Technorati
  • Agregar a Del.icio.us
  • Agregar a DiggIt!
  • Agregar a Yahoo!
  • Agregar a Google
  • Agregar a Meneame
  • Agregar a Furl
  • Agregar a Reddit
  • Agregar a Magnolia
  • Agregar a Blinklist
  • Agregar a Blogmarks

Hi Friends,

What is REST ?
Rest is a principle followed by nowadays application architecture like MVC. its stands for representational state transfer which means
a)URL should be Identity to Operation which is Performing in Application.
b)URL should be in Human readable format so that anyone can get the idea of operation easily.

Example of Restful Urls 


Basically there are verbs associated with each operation for a particular url

eg:
I am going to explain about my practical example nowonwards which is really easy to understand.
Suppose I am Having a Controller Named Server.
Now for there could be following Scenario :

Server/ServerList   //displays List of all available servers.

Server/id/ClientList //displays List of all available clients for a particular server using its id i.e. serverid.

Server/id/DeleteServer //deletes a particular server using its id.

Server/id/DeleteClient //deletes all those clients which are associated with server for its id.

Here is how my controller is looking for all its actions with parameters,verbs they are accepting.



















Now For these action to execute we need to add views.

I used Model binding concept of Asp.net MVC to do this task and bind ServerList and ClientList with
IEnumerable<T> and AddServer/AddClient with <T>

for ServerList  Iterate through records as follows :

















same Model binding for ClientList View Page.

For AddServer Bind view with <T> as follows :

















same done for AddClient.

Benefit of doing so is you  need not to pass each parameter in function when POST is used to Create new Item,and need not to add namespace for particular model and add its fields in view,Just Iterate through ViewData.Model.

You are almost done with Rest Now Take a look into one of the great feature of MVC Architecture
Routing 
you need to specify route for above urls.

so I added the following routing in my global.asax file.


 routes.MapRoute(
           "Default", // Route name
           "Server/{id}/{action}", // URL with parameters
           new { controller = "Server",id=UrlParameter.Optional ,action = "ServerLists" } // Parameter defaults
       );

by doing so this will accepts all request for above urls.

Trick with routing : Specify More Specific Routes above Less Specific

consider a scenario : 

Suppose I am having a doubt in above urls and I added a route for AddServer,AddClient as follows :

  routes.MapRoute(
           "CustomRouteFirst", // Route name
           "Server{action}", // URL with parameters
           new { controller = "Server",action = "AddServer" } // Parameter defaults
       );
now this will match Server/AddServer and Server/AddClient but what about my other routes ?
my others urls will be treated according to this routes and consider my DeleteServer Url would be now.
Server/DeleteServer?Id=(myserverid)
which i really don't want. I need this to be Server/id/DeleteServer.

Consider another example : 

Take a look into this route first :

//[Priority]:First


routes.MapRoute(
           "SpecialRoute", // Route name
           "Category/{categorytype}", // URL with parameters
           new { controller = "Product",action = "List" } // Parameter defaults
       );

// [Priority]:Second

routes.MapRoute(
           "CustomRouteSecond", // Route name
           "{controller}/{action}/{id}", // URL with parameters
           new { controller = "Product",action = "Index",id=UrlParameter.Option } // Parameter defaults
       );


so when one request for  Category/mobilephone //this will display all mobile phone lists.
and when someone request for Product/id/Delete //in this case First route won't match and hence its comes to down for second route and transfer the state to appropriate action.

 if you swap these routes priorities  then what would be scenario.

a)for Product/id/Delete url this will go for appropriate action.
b)for Category/mobilephone this will again match with the first route(ie. generic route) and trying to go for appropriate action which is not there & hence page not found error message will appear.
Hope ! you enjoyed this article.

0 comments: