Letting the RenderMvcController catch all sub urls
I would like to be able to do something like this:
I have some umbraco pages:
/en/products /da/produkter
These pages are both of "Products", and I have defined a ProductsController : RenderMvcController in my application, that hijacks the route fro those pages.
Now I woul like to catch these url's with that controller:
UrlRewriting is probably not the way to go, since I don't know the base url up front, as it may be localized.
I could maybe set up an INotFound handler, that trims off the end of the url until it matches a node, and the add the rest of the url to the context. Then my Controller would need to delegate to the appropriate actions itself, based on what is in the context.
Any other options? Any way to make that a feature of the core (or maybe it already is?)?
Or, with the new routing logic you'd be able to define your own content lookup and register it on app startup, you'd need to ask stephen about that though.
It should be pretty easy just to create your own routes though.
Ensure that you have a way to identify the "products" and "produkter" nodes eg by having them being of a ProductsPage content type.
In the IContentFinder, enumerate those nodes and check whether the requested uri starts with the node uri. When that's the case, set the request's published content to the node in question. That does not change the request's uri so the rest of the uri (category, sku) still remains available (no need to put in in a context).
You may, or may not want to set a template on the node itself. If there's no template then the Index method of the Controller will trigger, and there you can route to your overloaded methods based upon category, sku... and pick the right view.
No, there's no magic anymore. You need to explicitely register it. I recommend you browse the slides of my excellent CodeGarden '13 presentation ;-)
Otherwise:
public class MyApplication : ApplicationEventHandler { protected override void ApplicationStarting(…) { // Insert my finder in first position ContentFinderResolver.Current .InsertType<MyContentFinder>(); } }
And that one will be picked and run automagically.
Letting the RenderMvcController catch all sub urls
I would like to be able to do something like this:
I have some umbraco pages:
/en/products
/da/produkter
These pages are both of "Products", and I have defined a ProductsController : RenderMvcController in my application, that hijacks the route fro those pages.
Now I woul like to catch these url's with that controller:
/en/products/{category}
/en/products/{category}/{sku}
/da/produkter{category}
/da/produkter{category}/{sku}
And it would be really awesome if that was routed to my appropriate actions:
Index(RenderModel model)
Index(RenderModel model, string category)
Index(RenderModel model, string category, string sku)
How would I achieve this?
UrlRewriting is probably not the way to go, since I don't know the base url up front, as it may be localized.
I could maybe set up an INotFound handler, that trims off the end of the url until it matches a node, and the add the rest of the url to the context. Then my Controller would need to delegate to the appropriate actions itself, based on what is in the context.
Any other options? Any way to make that a feature of the core (or maybe it already is?)?
EDIT: I'm using v 6.1.1
Why not just setup a custom route like normal MVC? Then lookup what you want and return a RenderModel
Or, with the new routing logic you'd be able to define your own content lookup and register it on app startup, you'd need to ask stephen about that though.
It should be pretty easy just to create your own routes though.
Create an IContentFinder, no an INotFoundHandler.
Ensure that you have a way to identify the "products" and "produkter" nodes eg by having them being of a ProductsPage content type.
In the IContentFinder, enumerate those nodes and check whether the requested uri starts with the node uri. When that's the case, set the request's published content to the node in question. That does not change the request's uri so the rest of the uri (category, sku) still remains available (no need to put in in a context).
You may, or may not want to set a template on the node itself. If there's no template then the Index method of the Controller will trigger, and there you can route to your overloaded methods based upon category, sku... and pick the right view.
Making sense?
Yes, makes sense with the custom IContentFinder.
Do I just need to implement it, and it will be picked up automagically?
No, there's no magic anymore. You need to explicitely register it. I recommend you browse the slides of my excellent CodeGarden '13 presentation ;-)
Otherwise:
And that one will be picked and run automagically.
Stephan
Argh, I knew I would miss something by not being at CG :)
Thanks again.
Hi Morten,
Could you post some example code when you have everything working? Would like to see how it's working with a real world example :-).
Jeroen
Hi Jeroen
I will try and get to it, if I end up using this approach. (There are a few factors to count in)
But I might do a demo of it just for kicks. Then i'll make a repo with it.
is working on a reply...