Resources Over MVC - Bare Minimum Part 1

October 31, 2011 21:00 by Admin

This series of posts is introducing the features in the ROM code. If you haven’t been there already, take a look at the introduction and perhaps download the source from CodePlex. The absolute minimum you need to get going with Resources Over MVC (ROM) is an existing ASP.Net MVC web site, a reference to the ROM assembly (via NuGet) and a single line of code.

Serving Multiple Representations

One of the features people associate with a RESTful web service is the ability to serve multiple representations of the same resource. Perhaps one User Agent prefers to talk XML (often server to server) whereas another prefers JSON (for example JavaScript running in a browser). To get that capability in your web site (read only) you can use an action filter that comes as part of the ROM assembly using these two steps:

  1. Add a reference to the Resources Over MVC assembly to the project (you can do this through NuGet or download it from CodePlex).
  2. Hook up ROM’s Rest Enabled action filter by either:
    • Adding a [MultipleRepresentations] attribute to each action you want to support multiple representations or
    • Adding a [MultipleRepresentations] attribute to each Controller you want to support multiple representations on all its Actions or
    • Adding it as a global filter on Application start-up so that all Actions in all Controllers support multiple representations. You do this by using the following in the Application_Start method in Global.asax:
                filters.Add(new MultipleRepresentationsAttribute());

 

So if you have an existing web site, into which you want to incorporate a simple web service, that can serve read only data in different formats, create a new Controller and add the [MultipleRepresentations] attribute to it. That is all!

Trying it Out

Now, if a User Agent sends a request having set an Accept header such as:

   GET http://mymachine/services/products/ HTTP/1.0
Accept: application/xml
Host: mymachine

The web service will return:

   HTTP/1.1 200 OK
   Cache-Control: private
   Content-Length: 398
   Content-Type: application/xml; charset=utf-8
   Server: Microsoft-IIS/7.5
   X-AspNet-Version: 4.0.30319
   X-AspNetMvc-Version: 3.0
   X-Powered-By: ASP.NET
   Date: Thu, 02 Jun 2011 20:01:29 GMT
   Connection: close

   <?xml version="1.0" encoding="utf-8"?>
   <ArrayOfProduct>
    <Product>
      <Uri>http://mymachine/Services/Products/0</Uri>
      <Id>0</Id>
      <Name>Coke</Name>
      <UnitPrice>1.25</UnitPrice>
      <UnitsInStock>101</UnitsInStock>
    </Product>
    <Product>
      ...

You can try this using a tool such as Fiddler which allows you to ask for different representations.

Note: If you do use Fiddler, you will need to ensure your project is set up to use your local IIS Web Server rather than the Visual Studio Development Server (right click the project, select Properties… then go to the Web tab). Then in Fiddler use the Request Builder to perform a GET against the web service (using the machine name rather than localhost in the URI) with an Accept header set to “application/xml”.

If you try setting the Accept Header to “application/json” and you will get this response:

   HTTP/1.1 200 OK
   Cache-Control: private
   Content-Length: 203
   Content-Type: application/json; charset=utf-8
   Server: Microsoft-IIS/7.5
   X-AspNet-Version: 4.0.30319
   X-AspNetMvc-Version: 3.0
   X-Powered-By: ASP.NET
   Date: Thu, 02 Jun 2011 20:14:03 GMT
   Connection: close
[ { "Uri":"http://mymachine/Services/Products/0", "Id":0, "Name":"Coke", "UnitPrice":1.25, "UnitsInStock":101 }, { ...

Note: In the above samples the server has provided a response using the default UTF-8 encoding. The actual encoding of the response can be set by the User Agent via the Accept-Charset header. The processing of both the Accept and Accept-Charset headers support the quality factor parameter.

What’s Next

The next post (Bare Minimum Part 2) will show how you can have your web service receive data in different formats… again by adding a single line of code. It will also include a downloadable sample project. Future posts will build a web service from scratch and introduce other features of the assembly.


Add comment

  Country flag

biuquote
  • Comment
  • Preview
Loading