Copied to clipboard

Flag this post as spam?

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


  • Nigel Wilson 945 posts 2077 karma points
    Aug 03, 2018 @ 19:25
    Nigel Wilson
    0

    Default Custom Controller & Having An Action On All Pages

    Hi there

    I have read and successfully implemented a default custom controller and I "hit" the controller when viewing pages where there is no associated page controller.

    However the site I am building needs some functionality to run on all pages, and whilst I can add the logic to all page controllers as required, I wondered if there is a way to have it fire automatically on all pages ?

    Thanks

    Nigel

  • David Brendel 792 posts 2970 karma points MVP 3x c-trib
    Aug 04, 2018 @ 19:25
    David Brendel
    100

    Hi Nigel,

    thought on creating a base controller that you can use for all your pages to inherit from?

    Or maybe try to implement an ActionFilter which runs on each request.

    Regards David

  • Nigel Wilson 945 posts 2077 karma points
    Aug 04, 2018 @ 19:35
    Nigel Wilson
    0

    Hi David

    Thanks for your thoughts - appreciate it.

    ActionFilters are new to me so will investigate.

    Re the BaseContoller controller idea - so conceptually a Base Controller that inherits from RenderMVCController, with the individual controllers inheriting from the Base Controller ?

    Cheers

    Nigel

  • David Brendel 792 posts 2970 karma points MVP 3x c-trib
    Aug 04, 2018 @ 19:42
    David Brendel
    0

    Hi Nigel,

    the ActionFilter stuff is quite nice to handle things that should be run on each request. Depends on what you are trying to accomplish.

    Yes exactly, create a BaseController which inherits from RenderMvcController and then let all your custom controller inherit from that BaseController.

    Regards David

  • Nigel Wilson 945 posts 2077 karma points
    Aug 05, 2018 @ 19:08
    Nigel Wilson
    0

    Hi David

    Added the base controller logic last night and it is working a treat so far.

    APpreciate your feedback / help. Will still have a look at ActionFilters too - always good to know about other options.

    Cheers, Nigel

  • Nigel Wilson 945 posts 2077 karma points
    Aug 06, 2018 @ 07:29
    Nigel Wilson
    0

    David

    Sorry but it seems my excitement was a bit premature.

    Below is my base controller, but this is now creating a YSOD - when revert to "standard" umbraco controller code the site runs up fine.

    public class BaseController : RenderMvcController
        {
            protected override void Initialize(RequestContext requestContext)
            {
                //TODO - write Umbraco helper to get home page 
                //TODO - write logic to check user
    
            }
        }
    

    Can anyone enlighten as to how to create a BaseController that inherits from RenverMvController?

    Thanks Nigel

  • David Brendel 792 posts 2970 karma points MVP 3x c-trib
    Aug 06, 2018 @ 08:02
    David Brendel
    0

    Hi Nigel,

    what is the error message in the YSOD?

    Regards David

  • Nigel Wilson 945 posts 2077 karma points
    Aug 06, 2018 @ 08:39
    Nigel Wilson
    0

    Hey David

    OK, things are working now with updated code as below.

    public class BaseController : RenderMvcController
        {
    
            protected override void Initialize(RequestContext requestContext)
            {
                base.Initialize(requestContext);
    
            }
    
            protected override void OnActionExecuting(ActionExecutingContext filterContext)
            {
    
            }
    
        }
    

    I had left out the base.Initialize(requestContext) line, but both methods above are hit when debugging, so the train is back on the tracks and about to get a hot of coal - full steam ahead... :-)

    Cheers, Nigel

Please Sign in or register to post replies

Write your reply to:

Draft