I have been disappointed with the amount of legwork involved with setting up a simple REST API, so I decided to write a small attribute-based framework. The goal was to make it as simple as possible and be discoverable. Right now, the framework only supports GETting objects from your api, but that will change very soon.
Setting up RestLess
- Add a reference to RestLess.dll
- In web.config under <system.web>, add
<httpModules>
<add type="RestLess.RESTHttpModule, RestLess" name="RestLess" />
</httpModules>
- To tell RestLess where your API root directory is, in web.config under the <configuration> section add this key (you can make this whatever path you want)
<appSettings>
<add key="RestLess::BaseUrl" value="api/" />
</appSettings>
That's it!
Exposing an object via the api
You may have a Product object in your application with the properties Name, Description, and Amount. If you want to expose this via the api, you would simply decorate this class as follows. (RestLess code in blue, Comments in green)
// decorate the class as a RESTResource, the first parameter is the url of the resource appended to
// the root url (defined in web.config). In this case /api/products/
// the second parameter specifies if this resource is discoverable (more on this later)
[RESTResource("products", true)]
public class Product : IRESTReadable
{
private string _name;
private string _description;
private double _amount;
// render this property as xmlelement "productname"
// IOType.InputOutput specifies that this property will be rendered
// to the outputted xml, and can also be inputted via querystring
[RESTProperty("productname", IOType.InputOutput)]
public string Name
{
get { return _name; }
set { _name = value; }
}
// render this property as xmlelement "productdescription"
// IOType.Output specifies this property is rendered to the user,
// but it is not allowed in querystring for input
[RESTProperty("productdescription", IOType.Output)]
public string Description
{
get { return _description; }
set { _description = value; }
}
// render this property as xmlelement "amount" (output only again)
[RESTProperty("amount", IOType.Output)]
public double Amount
{
get { return _amount; }
set { _amount = value; }
}
#region IRESTReadable Members
public object GetRESTObject()
{
// this is where you could go access your data access layer or whatever other method you choose.
// this is also where you could request an API key via querystring for security etc
// since productname is specified inputoutput, you should check for
// Request.QueryString["productname"] to get any data the user sends
// for the purposes of this demo, lets just make an object and pass it back
Product p = new Product();
p.Name = "Super Fly Ink Pen";
p.Description = "This is the most super fly ink pen ever";
p.Amount = 10.00;
return p;
}
#endregion
}
Done - that's all you have to do! You can add these attributes to as many of your classes as you want to expose through your api.
If you request the url http://yoursite.com/api/products/default.aspx, the output will look like below

Discovering The API
As I mentioned earlier, the second parameter of the RESTResource attribute specifies if the resource is discoverable. If this parameter is set to true, you can visit http://yoursite.com/api/discover.aspx to see what the available resources are. The output of http://yoursite.com/api/discover.aspx will look something like below (notice another Customer object - I did not include this class for brevity).

I plan on making lots of updates and changes to the framework as needed (I am VERY open to suggestions!!). This is a rough-rough draft, but seems to work well.
... now go and add an API to your .NET application in a few minutes. :)
Jayme
Read the complete post at http://feeds.feedburner.com/~r/JaymeDavis/~3/273918717/