Sub-URLs

When creating a RESTful design it is natural to nest resources. A customer has orders. An invoice has line items. A blog post has comments. Snooze recognises this and makes it easy to express the relationships and avoid repetition.

Step 1: Create your Url classes. Use SubUrl<T> to express the relationship.

public class CustomerUrl : Url
{
    public string CustomerId { get; set; }
}

public class OrderUrl : SubUrl<CustomerUrl>
{
    public string OrderId { get; set; }
}

Step 2: Define the two routes.

routes.Map<CustomerUrl>(c => "customer/" + c.CustomerId);
routes.Map<OrderUrl>(o => "order/" + o.OrderId);

The sub-url route does not need to repeat the parent section of the url. It will be prepended by Snooze. Snooze will also put the "/" between the two sections.

Step 3: You can access the parent Url properties from the child Url, using the Parent property.

public ActionResult Get(OrderUrl url)
{
var customer = db.LoadCustomerById(url.Parent.CustomerId);
...
}

The Parent property is provided by the SubUrl<T> base class.

You can nest urls as deep as you like.

Creating Sub-Urls in Code

When generating a sub-url in code you can keep the code readable by using the Concat method.

var customerUrl = new CustomerUrl { CustomerId = 123 };
var orderUrl = customerUrl.Concat(new OrderUrl { OrderId = 890 });

Setting the Parent property is also an option, but I think the above looks nicer.