REST with .NET
This is my first attempt at creating a web service using REST and .NET. The easiest thing I could come up with was to implement the IHttpHandler interface and add my custom processing logic to the ProcessRequest method. The ItemManager class contains my XML data that I am returning to the client.
public class ItemHttpHandler : IHttpHandler
{
public ItemHttpHandler()
{
}
#region IHttpHandler Members
public bool IsReusable
{
get { return false; }
}
public void ProcessRequest(HttpContext context)
{
HttpRequest request = context.Request;
HttpResponse response = context.Response;
string command = ParseCommand(request.Url.Segments);
string result = ProcessCommand(command);
response.ContentType = “text/xml”;
response.Write(result);
}
private string ParseCommand(string [] path)
{
string command = path[path.Length - 1];
return command.Remove(command.Length – 5);
}
private string ProcessCommand(string command)
{
ItemManager gm = new ItemManager();
string result;
if (command == “list”)
{
result = gm.GetItemList();
}
else
{
int id = Int32.Parse(command);
result = gm.GetItemById(id);
}
return result;
}
#endregion
}
Here is how I registered my custom handler with ASP.NET.
<httphandlers>
<add verb=”*” path=”items/*.rest” type=”ItemHttpHandler”>
</add>
I still need to work out a couple of kinks. In order for a request to be processed by my custom handler, the URI needs to end with “.rest”. I haven’t been able to have an extensionless URI processed by a custom handler. I also haven’t experimented with using an HTTP POST to submit data to my web service. Currently it only returns data. As I figure these things out, I’ll try and post my solutions.
November 7th, 2006 at 12:04 pm
interesting, am trying to do the same thing at the moment, will keep watching this space to see how you get on.