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.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
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.

1
2
3
<httphandlers\>
<add verb="\*" path="items/\*.rest" type="ItemHttpHandler" />
</httphandlers>

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.