Copied to clipboard

Flag this post as spam?

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


  • Phill 115 posts 289 karma points
    Feb 18, 2016 @ 22:29
    Phill
    0

    Best Practice for Passing Parameter via URL to Page/SurfaceController?

    Hi there,

    I know there are a lot of posts about this and I've used different approaches successfully in the past, both URLRewrite rule and a custom ContentFinder. I'm starting another project and have the following requirement and just want to know what the best approach is.

    Scenario is I have a set of pages that use the same DocumentType. This DocumentType has a SurfaceController and in that surface controller I want to run some custom code based on a parameter passed via the url. So my urls would look like this: domain/page/doctypepage/id or domain/page/subpage/doctypepage/id etc...

    It seems like such a simple thing to just want to access a single value that's added to the end of the url so the process of creating custom routes, content finder etc all seems so complicated and I just wanted to check to see what others are doing for this type of scenario to make sure I'm not over thinking things?

    Thanks in advance for any feedback. Phill

  • Marc Goodson 2155 posts 14408 karma points MVP 9x c-trib
    Feb 27, 2016 @ 00:49
    Marc Goodson
    0

    Hi Phill

    Since your set of pages all use the same Document Type then this would be a good candidate for route hijacking requests made to this particular Document Type:

    https://our.umbraco.org/documentation/reference/routing/custom-controllers

    Create a blank MVC controller with the naming convention DocumentTypeAliasController and have it inherit from RenderMvcController; the create an Index ActionResult that expects to receive a RenderModel eg:

    public class DocTypeAliasController : Umbraco.Web.Mvc.RenderMvcController { public ActionResult Index(Umbraco.Web.Models.RenderModel model) { return CurrentTemplate(model); } }

    Now any request to a page based on your Document Type will be hijacked and call this controller / default index action instead.

    If your page is published with a template name alias 'standardPage' then you can create an ActionResult that matches this:

    public ActionResult StandardPage(Umbraco.Web.Models.RenderModel model, string myparameter)
        {
    

    // do something with myparameter // build a custom model that inherits from RenderModel etc return CurrentTemplate(model); }

    this will enable you to read a parameter on the querystring in the action result and action your custom code dependent on the value.

    You could also build a custom model here to use in your Views.

    regards

    Marc

Please Sign in or register to post replies

Write your reply to:

Draft