Skip to content

Instantly share code, notes, and snippets.

@darrelmiller
Created May 15, 2012 16:51
Show Gist options
  • Save darrelmiller/2703205 to your computer and use it in GitHub Desktop.
Save darrelmiller/2703205 to your computer and use it in GitHub Desktop.
Hierarchical MVC routing
var router = new ApiRouter("api").DispatchTo<RootController>()
.Add(new ApiRouter("Contacts").DispatchTo<ContactsController>())
.Add(new ApiRouter("Contact").WithHandler(new LoggingHandler())
.Add(new ApiRouter("{contactid}")
.WithConstraint("contactid", @"\d+")
.DispatchTo<ContactController>()
.Add(new ApiRouter("Address")
.Add(new ApiRouter("{addressid}")
.WithConstraint("addressid", @"\d+")
.DispatchTo<AddressController>()
)
)
)
);
// or
var router = new ApiRouter("api").DispatchTo<RootController>()
.Add(new ApiRouter("Contacts").DispatchTo<ContactsController>())
.Add(new ApiRouter("Contact({contactid})")
.WithConstraint("contactid", @"\d+")
.WithHandler(new LoggingHandler())
.DispatchTo<ContactController>()
.Add(new ApiRouter("Address({addressid})")
.WithConstraint("addressid", @"\d+")
.DispatchTo<AddressController>()
)
);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment