I have released an initial version of Snooze. http://www.assembla.com/wiki/show/snooze
Snooze is an REST framework for ASP.NET. It puts the emphasis back on the "R" of URI. The Resource concept is a first-class citizen.
In Snooze you create a resource class that defines the URI template and HTTP methods it supports.
[HttpResource("customer/{CustomerId}")] // Defines the URI template for this resourcepublic class CustomerResource : HttpResource // A useful base class that implements IHttpResource{ // URI template variables are mapped to properties public int CustomerId { get; set; } // Handles the HTTP GET method public override void Get() { RenderView("Customer", Data.Customers[CustomerId]); } public override void Put() { // Read data sent in Request body and update the customer... } public override void Delete() { // Delete the customer... } // HTTP POST can also be provided if it makes sense for the resource.}
This will mean a URI like "http://yourserver.com/customer/42" will invoke the Get method of an instance where CustomerId equals 42.
What about creating a new Customer?
Well you don't have an ID for the new customer since that is probably created in the database. Therefore you don't know what the URI will be yet. The correct solution here is to POST the new customer data to a CustomerList resource, with URI "/customers".
You can create extra GET methods:
[HttpGetView("edit")]public void GetEdit(){ RenderView("CustomerEdit", Data.Customers[CustomerId]);}
This results in URIs like "/customer/42?edit"
To support different file formats, such as XML and JSON use:
This will be mapped to by URIs like /customer/42.xml
Resources can be naturally nested. For example, a Customer may have Orders.
[HttpSubResource(typeof(CustomerResource), "order/{OrderId}", true)]public class OrderResource : HttpSubResource<CustomerResource>{ public int OrderId { get; set; } // ... }
In this sub-resource, you can have access the parent CustomerResource, using the strongly-typed, ParentResource property.
URIs for this sub-resource will look like: "/customer/42/order/123"
The "true" argument in the attribute, specifies that parent query string parameters are added to this sub-resource URI as well. So a parent with URI template "customer/{CustomerId}?foo={Foo}" will yield sub-resource URI template "/customer/{CustomerId}/order/{OrderId}?foo={Foo}".
Imagine a blog posts resource. Its URI could be /posts/{Year}/{Month}/{Day} to get all the posts on a given date. Now what if you want to naturally get the posts for a whole month, year, or ever?
By declaring the properties as nullable, e.g. "int?" in C#, Snooze will also map the "reduced" URIs to your class. You can then test the properties for "null" and return data accordingly.
When rendering an HTML view it is likely you will need to link to other resources. Instead of manually typing the URI string, you can call the Uri property available on HttpResource.
new CustomerResource { CustomerId = 42 } .Uri
Snooze supports resource "areas". These let you map all the resources of a given assembly into a given sub-path. If, for example, a third-party blogging package was built using Snooze you could include it as follows:
web.config:
<configuration> <restAreas> <area path="" assembly="MyWebsiteAssembly"/> <area path="blog" assembly="SomeCompany.BlogEngine, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null"/> </restAreas>
So the blog resource URIs would look something like: http://mysite.com/blog/posts/2007/01
HTML forms only support GET and POST. To overcome this limitation you can include: <input type="hidden" name="__method" value="DELETE" /> to override the name of the HTTP method used by Snooze.
This project came mostly out of curiosity! I wanted to see if REST could be done better under ASP.NET. The code is still very new (less than two days!) It is open source (BSD license) and available via SubVersion. A quick sample project is also provided.
Let me know what you think. Everything is open to changes. Join the assembla project if you would like to contribute your help.
Remember Me
© Copyright 2008 Andrew Davey Theme Design by Bryan Bell newtelligence dasBlog 1.9.6264.0 | Page rendered at Wednesday, August 20, 2008 8:08:38 AM (GMT Standard Time, UTC+00:00) Pick a theme: BlogXP calmBlue Candid Blue dasBlog Discreet Blog Blue Elegante essence Just Html MadsSimple Mobile Mono Movable Radio Blue Movable Radio Heat nautica022 orangeCream