Creating a RESTful Web Service Using ASP.Net MVC Part 18 – Tidying Up

November 23, 2009 22:38 by Admin

Over the last year I written a whole series of articles looking at creating a RESTful web service. For some time I’ve been meaning to tidy up the DLL that provides the RESTful functionality… and finally I’ve got round to it!

After using my DLL to implement a couple of real life web services, I made some changes:

  • Removed the hardcoded external namespace from the DLL
  • Moved ErrorController out of the DLL
  • Corrected some XSLT errors that had crept in
  • Made the home page support multiple formats
  • Become a bit les precious about using namespaces internally

Having my DLL impose its hardcoded namespace on everybody was obviously not going to work in the real world, so it had to go. Moving the namespace out pretty much dictated the ErrorController should move out as well… otherwise each controller derived from RestfulController would have to provide overrides of an abstract method in order to allow errors caught within the RestfulController to be handled gracefully. Instead I:

  • Changed the RestfulController to raise HttpExceptions instead of trying to handle errors itself;
  • Centralised catching of exceptions within Global.asax.cs. The Application_Error method now catches all errors and passes them to the new ErrorController;
  • From the ErrorController, supply additional information to the Views if the debug flag is set to true in web.config… without this, finding problems is tedious as the previous error handling (correctly) hid away all interesting information about why the service was not working.

As I made the changes, I realised some errors had crept into my XSLT. So anybody downloading the previous version would have been underwhelmed!

I realised I had not made the Home page RESTful. Since this is where any real client would pick up its list of available resources, I updated it to return a list of resources and their URIs. All the usual formats are supported.

After working on a couple of real life implementations I have become more pragmatic about using namespaces inside my applications. Working without namespaces is just simpler! So the new XSLT required for the Home page does not expect the internal representation to have a namespace. This allows me to use the generic List<Resource> directly without having to wrap it in another class that can set its namespace. Also all the selectors in the XSLT are shorter, not needing as they no longer need namespace qualifiers.

However, the downside is that, because we no longer have the default namespace set to the external namespace, every element that is to be created in the output must be defined with a namespace. I found that the only way to make the output correct was to use the namespace attribute. I declared a variable (extNamespace) to contain the namespace so I didn’t have to keep copying it:

<xsl:stylesheet xmlns:xsl=""     xmlns:soge="" version="1.0">
  <xsl:output method="xml" indent="yes" encoding="utf-8"/>
  <xsl:variable name="extNamespace"></xsl:variable>

    <xsl:template match='ArrayOfResource'>
        <xsl:element name='ResourceCollection' namespace="{$extNamespace}">
            <xsl:apply-templates select='Resource' />

    <xsl:template match='Resource'>
        <xsl:element name='Resource' namespace="{$extNamespace}">
            <xsl:attribute name='id'>
                <xsl:value-of select='@identifier'></xsl:value-of>
            <xsl:attribute name='uri'>
                <xsl:value-of select='@uri'></xsl:value-of>


The latest version can is here: (98.88 kb)

Hopefully I’ll now have a little time to take on board some of the ideas I picked up from the REST for MVC SDK… starting with a different approach to handling overloading of POST.

kick it on

Add comment

  Country flag

  • Comment
  • Preview