Copied to clipboard

Flag this post as spam?

This post will be reported to the moderators as potential spam to be looked at


  • Tom Fulton 2030 posts 4998 karma points c-trib
    Apr 30, 2013 @ 19:30
    Tom Fulton
    0

    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

     

  • Stephen 767 posts 2273 karma points c-trib
    Apr 30, 2013 @ 21:19
    Stephen
    0

    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)...

  • Tom Fulton 2030 posts 4998 karma points c-trib
    Apr 30, 2013 @ 21:22
    Tom Fulton
    0

    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 :)

  • Stephen 767 posts 2273 karma points c-trib
    Apr 30, 2013 @ 21:33
    Stephen
    0

    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...).

Please Sign in or register to post replies

Write your reply to:

Draft