This is a library for developing a REST API in your web application on the ASP.NET framework.
The source for the library can be found here
HeavyMetal REST Library.
What it does
The library essentially will take any class marked with some specific attributes and expose it's methods via REST through your ASP.NET application. I'm not going to go into a description of what REST is, or it's merits over SOAP, I'll leave that for you to research.
Usage of the library is simple. Download and unzip the above archive. Add a reference to the project to your ASP.NET web application. Modify your web.config file to include the following lines:
<httphandlers>
<add verb="*" path="rest/*/*.ashx" type="HeavyMetal.REST.RESTHandler,HeavyMetal.REST"></add>
</httphandlers>
To expose a class and it's methods, simply modify your class by applying RESTClass and RESTMethod attributes. You can assign friendly names to the class and methods which affect the URL that is called to invoke the method. For example:
[HeavyMetal.REST.RESTClass]
public class RESTSample
{
public RESTSample() {}
[HeavyMetal.REST.RESTMethod]
public string HelloWorld(string greetings)
{
return "Hello there "+greetings;
}
}
In the above example, we've declared the class
RESTSample as a REST class by applying the
RESTClass attribute.
You can now call this method:
http://localhost/yoursite/rest/RESTSample/HelloWorld.ashx?greetings=Bob
If we want to mask, or change, the URL to be more friendly, we could redeclare the class in this way:
[HeavyMetal.REST.RESTClass("Test")]
public class RESTSample
{
public RESTSample() {}
[HeavyMetal.REST.RESTMethod("Hello")]
public string HelloWorld(string greetings)
{
return "Hello there "+greetings;
}
}
The URL for invoking the HelloWorld method has now changed to:
http://localhost/yoursite/rest/Test/Hello.ashx?greetings=Bob
If we were to go this URL, we'd get the following back:
<response>Hello there Bob</response>
How it Works
An HttpHandler intercepts requests to the webserver that call anything in the path "~/rest/". The first time a REST method is invoked, the handler iterates through all of the loaded assemblies and all of the assemblies in the web app's bin folder. It finds any class marked with the RESTClass attribute and adds it to an internal hashtable.
The HttpHandler then parses the path of the query, does a lookup on the hashtable to find a class matching the name and then looks to find a method matching the name. If it finds this, it will use reflection to invoke the method, passing in the parameters passed in the query string.
Right now, parameters for methods can only be simple types; e.g. int, bool, string, datetime, etc. However, the method itself can return simple types and classes. If returning a class from a method, make sure it has a
Serializable attribute applied. For example:
[Serializable]
public class Person
{
public string Name;
public int Age;
}
[HeavyMetal.REST.RESTClass("Test")]
public class RESTSample
{
public RESTSample() {}
[HeavyMetal.REST.RESTMethod]
public Person GetPerson(string who, int age)
{
Person result=new Person();
result.Name=who;
result.Age=age;
return result;
}
}
Now if we call this REST method:
http://localhost/yoursite/rest/Test/GetPerson.ashx?who=Jon&age=32
Will return:
<Person>
<Name>Jon</Name>
<Age>32</Age>
</Person>
Notes
This is a really rough version. If you have any ideas, bugs, patches, please email them to me
here.
Jon Gilkison
interfacelab.com