Creating a RESTful Web Service Using ASP.Net MVC Part 2 – A Basic Resource

September 30, 2008 00:06 by admin

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:

  1. Replace any “GUIDs” supplied by clients with real ones;
  2. 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++)




    return result;


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">

    <ul class="guids">

    <% 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.

Some people may feel a little cheated by now… all I’ve done is create a normal MVC web page, using nothing but the default features… I haven’t even changed the routing table entries! For those people, think again. It doesn’t have to be a browser that uses the above URIs. From JavaScript, the XMLHttpRequest object could make the same call and what it would retrieve is XHTML, a snippet of which is below:

<ul class="guids">












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.

Comments (2) -

July 9. 2010 10:27

Peter Bremer

Compiler Error Message: CS1579: foreach statement cannot operate on variables of type 'MvcApplication1.Models.GuidGenerator' because 'MvcApplication1.Models.GuidGenerator' does not contain a public definition for 'GetEnumerator'

Also, you are skipping quite a few important details. For example that you're creating a strongly typed view, and that it is named "Guid" instead of the default "Index"...

Peter Bremer

July 9. 2010 11:46


I'm no longer actively maintaining these older versions so I can believe they won't compile using later tools... I'd suggest only using them for reference. If you want working code, skip ahead to the latest posts.

The change to a strongly typed view came as a revision (4 months later) to the original post. I'm glad you figured it out!


Add comment

  Country flag

  • Comment
  • Preview