Copied to clipboard

Flag this post as spam?

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


  • kris-guy 3 posts 23 karma points
    Jun 08, 2022 @ 10:11
    kris-guy
    0

    OnActionExecuting / OnActionExecuted in hijacked controllers

    Hey folks,

    I'm trying to modify the viewmodel after an action in a hijacked controller, but OnActionExecuted (or OnActionExecuting) methods are never reached/fired.

    Example of a hijacked controller:

    public class PageHomeController : RenderController
    {
        public PageHomeController(
            ILogger<RenderController> logger,
            ICompositeViewEngine compositeViewEngine,
            IUmbracoContextAccessor umbracoContextAccessor)
            : base(logger, compositeViewEngine, umbracoContextAccessor)
        {
        }
    
        public override IActionResult Index()
        {
            return CurrentTemplate(CurrentPage);
        }
    
        public override void OnActionExecuting(ActionExecutingContext filterContext)
        {
            base.OnActionExecuting(filterContext);
        }
    
        public override void OnActionExecuted(ActionExecutedContext filterContext)
        {
            base.OnActionExecuted(filterContext);
        }
    }
    

    I have also tried implementing the OnActionExecuting/Executed in a custom RenderController without success.

    These are firing/working as expected in a custom controller.

  • Miguel Lopez 15 posts 117 karma points
    Apr 04, 2023 @ 11:52
    Miguel Lopez
    0

    Got the same issue. I haven't been able to get these methods firing, but managed to do a workaround by using a custom action filter:

    public internal class ControllerBaseActionAttribute : ActionFilterAttribute {
           public override void OnResultExecuting(ResultExecutingContext context)
            {
                var result = context.Result as ViewResult;
                var viewModel = result?.ViewData.Model;
    
                base.OnResultExecuting(context);
            }
    }
    

    Then, decorate the controller with the custom filter

    [ControllerBaseAction]
    public class PageHomeController : RenderController
    

    Hopefully someone else can find a solution to get the action methods fired.

  • Martin Rhodes 13 posts 131 karma points
    Apr 21, 2023 @ 11:37
    Martin Rhodes
    0

    In v10.4.0 at least, you can do

    public override async Task OnActionExecutionAsync(ActionExecutingContext context, ActionExecutionDelegate next)
    {
    }
    
  • This forum is in read-only mode while we transition to the new forum.

    You can continue this topic on the new forum by tapping the "Continue discussion" link below.

Please Sign in or register to post replies