Click here to Skip to main content
14,297,293 members

Build ReST based Web Services in .NET/C#

Rate this:
5.00 (12 votes)
Please Sign up or sign in to vote.
5.00 (12 votes)
12 Jul 2009CPOL
A ReST based Web Service for C#.


There are various examples of ReST available on the net like Web-Services for .NET/C#, but none of them provide a simple .NETish framework. For example, how easy is it to create a Web-Service by just deriving from a class and annotating methods as exposed for the web? This article provides a small framework for ReST based Web-Services primarily targeted for .NET versions 3.5 and older.

Using the code

Like any other article on the web about ReST and .NET, this implementation involves handling specific resources request through an IHttpHandler; in this case, it is PoC.Web.Services.ReSTServiceHandler. This IHttpHandler would handle the ReST calls (Get, Put, Post, and Delete) and dispatch them appropriately. In order to add the handler, the following lines must be added to the web.config:

        <add verb="*" path="RESTService/*" 

          type="PoC.Web.Services.ReSTServiceHandler, PoC.Web.ReSTService"/>
        <add verb="*" path="RESTService/*/*" 

          type="PoC.Web.Services.ReSTServiceHandler, PoC.Web.ReSTService"/>

The library provides two mechanisms for ReST Web-Services:

  1. IReSTService
  2. Same as the Web-Service model. Annotate all the methods of a class which need to be exposed.

  3. IReSTFulService
  4. Provides four calls to handle a request: Get, Put, Post, and Delete.

IReSTService based Web-Service

An example of an IReSTService is shown below:

public class Calculator : PoC.Web.Services.IReSTService
    public int Sum(int a, int b)
        return a + b;
    public int Multiply(int a, int b)
        return a * b;
    public int Subtract(int a, int b)
        return a - b;

In this example, we have a Calculator class which implements IReSTService and exposes three methods by annotating them with the ReSTMethod attribute. The ReSTMethod attribute requires an allowed verb on that method. That's it. If built, the CalculatorService can now be accessed by these URLs:

  1. Sum URL: http://<servername/localhost>/<web app>/RESTService/Calculator/Sum?a=-1&b=2
  2. Multiply URL: http:/<servername/localhost>/<web app>/RESTService/Calculator/Multiply?a=195&b=21
  3. Subtract URL: http:/<servername/localhost>/<web app>/RESTService/Calculator/Subtract?a=-12&b=2

Here is another example:

public class ShowNTell : PoC.Web.Services.IReSTService
    XmlDocument dom = new XmlDocument();
    public ShowNTell()
        dom.LoadXml("<Root><Child><SubChild>" + 
                    "<Parameter></Parameter><MagicNumber>" + 
                    System.Guid.NewGuid().ToString("N") + 
                    "</MagicNumber></SubChild>" + 
    public Employee[] Object2Xml()
        Employee e1 = new Employee();
        e1.Name = "Aremac Nokin ";
        e1.Id = "11201";
        e1.Address = "123 Summer Drive";
        e1.City = "Brahman";
        e1.Zip = "00001";
        e1.JoiningDate = DateTime.Today.AddYears(-5).AddMonths(-3).AddDays(-20);
        e1.Salary = 100000.00;
        Employee e2 = new Employee();
        e2.Name = "Reyalp Sinnet";
        e2.Id = "11201";
        e2.Address = "123 Summer Drive";
        e2.City = "Brahman";
        e2.Zip = "00001";
        e2.JoiningDate = 
        e2.Salary = 200000.00;
        return new Employee[] { e1, e2 };
    public XmlNode ReturnNode(string some_param)
        dom.SelectSingleNode("//Root/Child/SubChild/Parameter").InnerText = some_param;
        return dom.SelectSingleNode("//Root/Child/SubChild");

    public XmlDocument ReturnDom(XmlDocument param_1, int param2)
        dom.SelectSingleNode("//Root/Child/SubChild/Parameter").InnerText = 
        if (param_1 != null && param_1.DocumentElement != null)
            XmlElement elem = dom.CreateElement("NewItem");
            elem.InnerXml = param_1.DocumentElement.OuterXml;
        return dom;

In this example, we are exposing an array of POCO, an XmlNode, and an XmlDocument. We also see a new attribute in this example, ReSTXmlResponse. This attribute makes the engine (IHttpHandler) return an XML representation of the object by serializing the object. If the return type of the method is typeof XmlNode, then the engine would return the OuterXml of the node and set the content-type to "xml". And yes, there is a special type converter for XmlDocument/XmlNode from string ;-).

IReSTFulService based Web-Service

This interface has the following methods:

  • object GetCall(string args, HttpContext context)
  • object PutCall(string args, HttpContext context)
  • object PostCall(string args, HttpContext context)
  • object DeleteCall(string args, HttpContext context)

An example is shown below:

public class Parts : PoC.Web.Services.IReSTFulService
    #region IReSTFulService Members

    public object GetCall(string arg, HttpContext context)
        if (string.IsNullOrEmpty(arg))
            return allParts; // typeof Parts[]
            return filteredParts; //typeof Parts[]

    public object PutCall(string arg, HttpContext context)
        Part ps = null;
        //Get Part
          ps = PoC.Web.Services.Utilities.TypeConversion.
        catch(Exception ex){ //Bad Stream
                throw new PoC.Web.Services.InternalErrorException(503, "Bad Stream");
        return UpdatePart(ps);// let's say string

    public object PostCall(string arg, HttpContext context)
        Part ps = null;
        //Get Part
            ps = PoC.Web.Services.Utilities.TypeConversion.
        catch (Exception ex)
        { //Bad Stream
            throw new PoC.Web.Services.InternalErrorException(503, "Bad Stream");
        return InsertPart(ps);


    public object DeleteCall(string arg, HttpContext context)
       throw new PoC.Web.Services.InternalErrorException(503, "Method Not Supported");


The "Parts" service can now be accessed through the following links:

  1. Link to get all Parts:
  2. URL: http://<Servername/localhost>/<Web App name>/RESTService/Parts

    HTTP method: Get

  3. Link to get a Part by ID:
  4. URL: http://<Servername/localhost>/<Web App name>/RESTService/Parts/1101

    HTTP method: Get

  5. Update a Part:
  6. URL: http://<Servername/localhost>/<Web App name>/RESTService/Parts

    HTTP method: Put

    Send Body: '<Part><Id>1103</Id><Name>somet part name</Name><Description>something ...</Description></Part>'

  7. Create a new Part:
  8. URL: http://<Servername/localhost>/<Web App name>/RESTService/Parts

    HTTP method: Post

    Send Body: '<Part><Id></Id><Name>somet part name</Name><Description>something ...</Description></Part>'

  9. Delete a Part:
  10. URL: http://<Servername/localhost>/<Web App name>/RESTService/Parts/1103

    HTTP method: Delete

    Send Body: '<AuthorizationCode></AuthorizationCode>'


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


About the Author

United States United States
Loves coding...

Comments and Discussions

GeneralHosting in Windows Service Pin
vanjaESS25-Jan-11 6:08
membervanjaESS25-Jan-11 6:08 
GeneralMy Vote of 5 Pin
Sluggish14-Dec-10 10:15
memberSluggish14-Dec-10 10:15 
QuestionSIX different classes = simple??? Pin
moonfly33322-Sep-10 13:35
membermoonfly33322-Sep-10 13:35 
AnswerRe: SIX different classes = simple??? Pin
AdamNThompson28-Jan-12 16:57
memberAdamNThompson28-Jan-12 16:57 
GeneralAny body knows Pin
robertox200422-Jul-10 23:12
memberrobertox200422-Jul-10 23:12 
AnswerRe: Any body knows Pin
thatraja10-Aug-10 21:21
protectorthatraja10-Aug-10 21:21 
GeneralCannot deploy in IIS 7 Pin
TillmanZ23-Feb-10 19:32
memberTillmanZ23-Feb-10 19:32 
GeneralAssigning Session ID REST Web Service Pin
Member 116029618-Feb-10 6:02
memberMember 116029618-Feb-10 6:02 
GeneralProblem in deploying REST service in IIS Pin
jimmyjos16-Oct-09 2:02
memberjimmyjos16-Oct-09 2:02 
GeneralRe: Problem in deploying REST service in IIS Pin
mdodich18-Nov-09 8:40
membermdodich18-Nov-09 8:40 
GeneralRe: Problem in deploying REST service in IIS Pin
Rafael Ferraro18-Jan-10 8:33
memberRafael Ferraro18-Jan-10 8:33 
GeneralRe: Problem in deploying REST service in IIS Pin
mdodich19-Jan-10 3:05
membermdodich19-Jan-10 3:05 
GeneralRe: Problem in deploying REST service in IIS Pin
____11-Feb-10 12:44
member____11-Feb-10 12:44 
Generaldirectly aceesing using URL in IE Pin
raam_kimi26-Aug-09 2:20
memberraam_kimi26-Aug-09 2:20 
GeneralRe: directly aceesing using URL in IE Pin
raam_kimi26-Aug-09 2:24
memberraam_kimi26-Aug-09 2:24 
QuestionIIS7 Pin
Kofi Sarfo13-Jul-09 20:32
memberKofi Sarfo13-Jul-09 20:32 
AnswerRe: IIS7 Pin
Parag.Gadkari14-Jul-09 17:12
memberParag.Gadkari14-Jul-09 17:12 
GeneralRe: IIS7 PinPopular
MichaelMerrell30-Jul-09 3:48
memberMichaelMerrell30-Jul-09 3:48 
GeneralRe: IIS7 Pin
mamidrx11-May-12 8:47
membermamidrx11-May-12 8:47 

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.

Posted 12 Jul 2009


55 bookmarked