MVC: Creating 'Virtual Pages' via Controllers/Routes instead of UrlRewriting
Hi MVC Gurus,
I'm trying to get up to speed with all of the Umbraco MVC niceness. In my current situation, I'm trying to use MVC to create "virtual" pages - ie pages that don't exist at the URLs I want, but will pull in content from elsewhere and render as if they do exist.
Normally I'd use URL Rewriting for this, but I'm trying to see if there's a more modern/better way to achieve this.
Here's an example of my structure:
Site 1 Homepage EventPlaceHolder Site 2 Homepage EventPlaceHolder Data Events Event 1 Event 2 Event 3
So when someone goes to site1.com/e/event-1, I want to grab the matching event from the Data node, pass it into the EventPlaceHolder page and render its template.
I have this mostly working by:
- Create a new SurfaceController - EventSurfaceController - Create a custom route - "e/{urlName}" that maps to this controller - In the controller, find the matching Data node based on the urlName, add to TempData - Return RedirectToUmbracoPage(1250) (the ID of the EventPlaceHolder page) - Access the event from TempData and do some rendering
This works great, BUT it changes the URL from /e/event-1 to /eventplaceholder since I'm doing a Redirect. So, is there a way I can do this without doing a Redirect so the virtual URL persists?
Or, is there a better way to achieve this? I'm not thrilled with the "EventPlaceHolder" approach, but I think that somehow I need to map the request to a content node, so navigation macros etc will render properly. Maybe I can select the Home node for rendering but dynamically choose a different template?
I wonder if some of the new stuff like IContentFinder or PublishedContentRequest.Prepared could help me out here?
Why don't you set EventPlaceHolder's url to, say, "event", then add a rewriting rule so that site1.com/event/event-1 is rewritten into site1.com/event?event=event-1, then in EventPlaceHolder's template, find the node matching event-1. That is, the node with url /data/events/event-1. I think uQuery.GetNodeIdByUrl("/data/events/event-1") should return the ID of that node and from that it should be easy to get the node object itself through UmbracoHelper.Content(id)...
Yeah, that's how I would normally do it and I don't have a problem doing it that way. I just thought with all the new fun things going on there might be a better/cooler way using some MVC magic :)
Alternatively, you could create an IContentFinder that would "find" document /data/events/event-1 when the inbound url is site1.com/event/event-1... and pick the appropriate template too, based upon the site (site-1 vs site-2...).
MVC: Creating 'Virtual Pages' via Controllers/Routes instead of UrlRewriting
Hi MVC Gurus,
I'm trying to get up to speed with all of the Umbraco MVC niceness. In my current situation, I'm trying to use MVC to create "virtual" pages - ie pages that don't exist at the URLs I want, but will pull in content from elsewhere and render as if they do exist.
Normally I'd use URL Rewriting for this, but I'm trying to see if there's a more modern/better way to achieve this.
Here's an example of my structure:
Site 1
Homepage
EventPlaceHolder
Site 2
Homepage
EventPlaceHolder
Data
Events
Event 1
Event 2
Event 3
So when someone goes to site1.com/e/event-1, I want to grab the matching event from the Data node, pass it into the EventPlaceHolder page and render its template.
I have this mostly working by:
- Create a new SurfaceController - EventSurfaceController
- Create a custom route - "e/{urlName}" that maps to this controller
- In the controller, find the matching Data node based on the urlName, add to TempData
- Return RedirectToUmbracoPage(1250) (the ID of the EventPlaceHolder page)
- Access the event from TempData and do some rendering
This works great, BUT it changes the URL from /e/event-1 to /eventplaceholder since I'm doing a Redirect. So, is there a way I can do this without doing a Redirect so the virtual URL persists?
Or, is there a better way to achieve this? I'm not thrilled with the "EventPlaceHolder" approach, but I think that somehow I need to map the request to a content node, so navigation macros etc will render properly. Maybe I can select the Home node for rendering but dynamically choose a different template?
I wonder if some of the new stuff like IContentFinder or PublishedContentRequest.Prepared could help me out here?
PS - I'm running the latest nightlies of 6.1 beta
Thanks!
Tom
Why don't you set EventPlaceHolder's url to, say, "event", then add a rewriting rule so that site1.com/event/event-1 is rewritten into site1.com/event?event=event-1, then in EventPlaceHolder's template, find the node matching event-1. That is, the node with url /data/events/event-1. I think uQuery.GetNodeIdByUrl("/data/events/event-1") should return the ID of that node and from that it should be easy to get the node object itself through UmbracoHelper.Content(id)...
Yeah, that's how I would normally do it and I don't have a problem doing it that way. I just thought with all the new fun things going on there might be a better/cooler way using some MVC magic :)
Alternatively, you could create an IContentFinder that would "find" document /data/events/event-1 when the inbound url is site1.com/event/event-1... and pick the appropriate template too, based upon the site (site-1 vs site-2...).
is working on a reply...