This project is read-only.

Initial steps

First two steps is activating the Rest http handler in your web.config.

that is done by adding the following items to your web.config:

<modules runAllManagedModulesForAllRequests="true" >
<remove name="WebDAVModule" />
<add name="Restful.Net" path="/addressbook/*" type="Restful.Net.RestHandler, Restful.Net" verb="*" />
<verbs allowUnlisted="true">
<add verb="PATCH" allowed="true" />
<add verb="POST" allowed="true" />
<add verb="PUT" allowed="true" />
<add verb="DELETE" allowed="true" />
<add verb="HEAD" allowed="true" />
<add verb="GET" allowed="true" />
<httpErrors errorMode="Detailed" />

Then you need to register the rest services you want to use. That needs to be done in the global.asax.cs class.

In the Global.Asax you create an instance of the RestRouter class. Give it your implementation of the IServiceFactory and the base url for which the service should be servicing rest requests.
The base url together with the Uri templates of the Rest services will make up the information needed for the framework to invoke the service methods that matches.

After you have created the router instance, you add one or more rest services to it by invoking the Add method and giving it the type of the service and the path prefix of all http requests.

Its also in the Global.Asax that you override the serializers if you are not happy with the two default provided.

public class Global : System.Web.HttpApplication
void Application_Start(object sender, EventArgs e)
RestRouter router = RestRouter.CreateInstance(new IOCContainerDummy(), new Uri("http://localhost:8080"));

Defining the rest service

A rest service can be any class. It does not have to implement any interface or any abstract class. All that is required is that the rest methods is marked up with the RestMethod attribute.

The example below is part of the code sample that is included, and as you can see its fairly simple, and also support etags which enables browser and proxy based caching to cache data if its not modified, causing the request to finish faster since no data has to be sent to the client.

Any errors you want to communicate to the clients is done via the WebFaultException that is the built in Exception in the System.ServiceModel.Web assembly. This makes it easy to communicate 404's, 400's etc.

[RestMethod(Verb = HttpVerb.Post, UriTemplate = "")]
public void PostContact(string ownerID, Contact contact)
db.CreateContact(ownerID, contact);

[RestMethod(Verb = HttpVerb.Put, UriTemplate = "/{contactid}")]
public void PutContact(string ownerID, long contactID, Contact contact)
db.UpdateContact(ownerID, contactID, contact);

[RestMethod(Verb = HttpVerb.Get, UriTemplate = "/{contactid}")]
public Contact GetContact(string ownerID, long contactID)
Contact contact = db.GetContact(ownerID, contactID);
if (!string.IsNullOrWhiteSpace(RestContext.Current.Request.IfNoneMatch))
if (contact.GetETag().Equals(RestContext.Current.Request.IfNoneMatch))
throw new WebFaultException(HttpStatusCode.NotModified);

RestContext.Current.Response.ETag = contact.GetETag();
return contact;

Last edited Apr 21, 2011 at 4:45 PM by bjornbouetsmith, version 1


lhking Apr 17, 2013 at 4:19 AM