In this post I want to get a basic web service up and running. I’ll start off simple with a read only feature. Many database backed .Net applications use GUIDs as identifiers for their entities. Unfortunately, a number of client technologies cannot generate truly unique identifiers, for instance Flash based clients. Two possible solutions are:
- Replace any “GUIDs” supplied by clients with real ones;
- Provide a way for those clients to obtain genuine GUIDs.
For this exercise I will create a service that allows a client to download up to a 100 GUIDs at a time which it can then cache and use as required. I started by installing Visual Studio 2008 SP1 and the ASP.Net MVC Preview 5. MVC installs some new project templates. I used the ASP.NET MVC Web Application template to create a standard project. I stared out by adding the following files:
- GuidController.cs was added to the Controller folder;
- GuidGenerator.cs was added to a newly created Model\Guid folder;
- Guid.aspx was added to a newly created View\Guid folder
In the initial version, GuidGenerator.cs had a single method which created a list of new GUIDs:
public static List<System.Guid> GenerateGuids(int count)
List<System.Guid> result = new List<System.Guid>();
for (int counter = 0; counter < count; counter++)
The Guid controller called the model and passed the results to the Guid.aspx view:
public ActionResult Index(int? count)
// Provide a default count and restrict to the range 1 - 100
int actualCount = count ?? 10;
actualCount = Math.Min(100, actualCount);
actualCount = actualCount >= 1 ? actualCount : 1;
// Generate the requested Guids
List<Guid> guidGeneratorResult = GuidGenerator.GenerateGuids(actualCount);
// Guids should be different every time some this is one result we don't want to cache
return View("Guid", guidGeneratorResult);
The aspx page does little more than loop over the list of Guids, adding each one as an item in an unordered list:
<asp:Content ID="MainContent" ContentPlaceHolderID="MainContent" runat="server">
<% foreach (var guid in ViewData.Model)
<li><%= guid.ToString().ToUpper() %></li>
<% } %>
Update 29 Jan 2009: In the above snippet I have changed ViewData.Model.Guids to ViewData.Model, the snippet was originally taken from the code for a later post so wouldn’t work. Also, since ASP.Net RC1, it is possible to remove “ViewData.” as well.
After building the project I could point my browser at:
and I see a list of GUIDs. Changing the URI to:
returns a longer list. So there you go… if you followed my steps above, you’ve created a RESTful web service using ASP.Net and MVC.
This XHTML could be loaded into an Xml parser and the GUIDs extracted very easily.
XHTML is a valid representation to return from a web service. In fact, in their book RESTful Web Services, Leonard Richardson and Sam Ruby describe why XHTML is their preferred representation. They argue that, whilst most web services return a custom XML vocabulary, it is usually not necessary. It is often better to reuse an existing standard, especially if it is widely adopted. XHTML has a very well known way to represent a list, so why not reuse it?
OK, so if I left it there, I think you could be justifiably disappointed. So my next post will start doing some real work… I’ll describe my framework for supporting multiple representations.