Click here to Skip to main content
15,881,882 members
Articles / Web Development / IIS

.NET 2.0 REST Service

Rate me:
Please Sign up or sign in to vote.
4.55/5 (10 votes)
6 Jan 2009CPOL3 min read 108.7K   2.2K   50   20
A working implementation of a REST service in .NET 2.0.

Introduction

For a couple of days, I've been trying to find an example of a REST web service written in .NET. Not being ASTORIA yet a mature framework, I'd rather get something based on an already released product, even if this would mean that I should sacrifice features or write a little more code.

Background

There are two very interesting articles published in this site: Everything about REST Web Services - What and how - Part 2 - Design and REST Web Services in ASP.NET 2.0 (C#), but none of them showed me a proper, full .NET implementation. So, I decided to write my own.

Everything about REST Web Services - What and how - Part 2 - Design explains in detail how to format a proper REST request, how to handle the different VERBs, and what kind of information should be passed to the service in each case. I encourage you to take a look at this article since I won't discuss those concepts again.

What I will provide you is a simple implementation of these requirements based on HTTP request handlers built into the .NET 2.0 framework. There are many other ways to do this. You could try with the HTTP modules, you can go for the WCF classes, and some others. I had to make a choice, and the aim was to come up with something that most people could use.

Using the code

The Visual Studio 2008 solution will be composed of a website that will provide the data service in accordance to the REST model and also a client.

The Web Service will have a web.config file that will map the service URL requests, and a (very simple) class for each verb handler. The POST and GET verbs may be sent from any web browser, so I'll be providing you a simple .html test page for this. For PUT and DELETE, I have built a custom client.

Finally, the Contacts.xml is our data source.

Create the Web Service

The first thing you'll need to set up the Web Service is to configure IIS to properly handle the requests. A basic configuration will be to remove all inherited handlers and set up your own. To do that, you'll need to edit your web.config like this...

XML
<configuration>
 <system.web>
  <httpHandlers>
   <clear />
   <add verb="PUT" path="*" validate="false" 
      type="RESTHandlers.PutHandler, RESTHandlers"/>
   <add verb="POST" path="*" validate="false" 
      type="RESTHandlers.PostHandler, RESTHandlers"/>
   <add verb="GET" path="*" validate="false" 
      type="RESTHandlers.GetHandler, RESTHandlers"/>
   <add verb="DELETE" path="*" validate="false" 
      type="RESTHandlers.DeleteHandler, RESTHandlers"/>
  </httpHandlers>
 </system.web>
</configuration> 

The next step is to create the handlers. This will be a basic task. Your classes should implement IHttpHandler and process the request. The basic structure should be something like this:

C#
public void ProcessRequest(HttpContext context)
{ 
  // a) Validate the request.
  // Remember, for example, that a PUT should not contain 
  // any query strings values.
  if(context.Request.QueryString != sting.Empty)
  { 
    SendClientTheError(); 
    return;
  }
  // b) Process the request.
  ... 
  // c) Send a response.
  HttpResponse response = context.Response;
  response.Write("OK");
}

That's all for the service...

The clients

There are two clients included as an example in this solution: a webpage and a console application.

A .htm file will show some samples of the GET and POST verbs sent both from <A> and <FORM> tags.

The console application will issue sample PUT and DELETE commands.

Points of interest

Please note that I did not write an exhaustive validation for the requests, and that the examples are not a complete scenario for the service. For example, I haven't wrote code to make a DELETE with a query string to test the error message. Also, when you implement a service like this, it would be wise to add some other validations, like:

  • Validate the request URI with more detail.
  • Validate the query string parameters.
  • Validate the XML received against a schema.
  • Any other validation on the request context like user, referrer, etc...

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Architect Carlos Salvatore
Argentina Argentina
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.
This is a Organisation (No members)


Comments and Discussions

 
Question500 Error Pin
Member 823914420-Jan-13 9:04
Member 823914420-Jan-13 9:04 
GeneralMy vote of 5 Pin
Dan Randolph22-Dec-11 18:26
Dan Randolph22-Dec-11 18:26 
GeneralMy vote of 4 Pin
Ramanjaneyulu Kondaru8-Nov-11 1:15
Ramanjaneyulu Kondaru8-Nov-11 1:15 
GeneralMy vote of 5 Pin
chenwei912027-Jul-11 17:29
chenwei912027-Jul-11 17:29 
GeneralASP.Net Handler REST Service Pin
gerrits14-Dec-10 22:08
gerrits14-Dec-10 22:08 
GeneralExample code missing!!! Pin
reviloliver29-Nov-10 9:15
reviloliver29-Nov-10 9:15 
GeneralThanks for example Pin
Dorothy211-Jul-10 11:01
Dorothy211-Jul-10 11:01 
QuestionHow to deploy to IIS? Pin
Ayasha Chen26-May-10 20:30
Ayasha Chen26-May-10 20:30 
AnswerRe: How to deploy to IIS? Pin
Member 33785491-Apr-11 8:40
Member 33785491-Apr-11 8:40 
GeneralRe: How to deploy to IIS? Pin
Ayasha Chen1-Apr-11 13:47
Ayasha Chen1-Apr-11 13:47 
AnswerRe: How to deploy to IIS 7? Pin
Dan Randolph22-Dec-11 19:01
Dan Randolph22-Dec-11 19:01 
GeneralRe: How to deploy to IIS 7? Pin
carlos@takeapps26-Dec-11 7:02
carlos@takeapps26-Dec-11 7:02 
QuestionHow to implement this service? Pin
kraftspl6-Jan-09 8:24
kraftspl6-Jan-09 8:24 
AnswerRe: How to implement this service? Pin
Igor_VK7-Jan-09 3:49
Igor_VK7-Jan-09 3:49 
GeneralRe: How to implement this service? Pin
kraftspl7-Jan-09 3:58
kraftspl7-Jan-09 3:58 
AnswerRe: How to implement this service? Pin
Igor_VK7-Jan-09 5:48
Igor_VK7-Jan-09 5:48 
AnswerRe: How to implement this service? Pin
carlos@takeapps7-Jan-09 7:05
carlos@takeapps7-Jan-09 7:05 
GeneralRe: How to implement this service? Pin
rudu21-Jan-09 6:16
rudu21-Jan-09 6:16 
GeneralRe: How to implement this service? Pin
carlos@takeapps21-Jan-09 11:29
carlos@takeapps21-Jan-09 11:29 
GeneralRe: How to implement this service? Pin
Wayk16-Feb-10 14:51
Wayk16-Feb-10 14:51 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.