How to redirect to a custom Page in case of Expired Page.

Vote:
 

Hello everyone.

I'm trying to do something that I'm not completly sure if I can do.

I have a Page that is Expired (Expiration Date added in the Epi CMS). the idea is that when a user will try to access this page be redirected to another Page automatically.

For do this I have this code on my PageControllerBase (or BaseController):

        protected override void OnAuthorization(AuthorizationContext filterContext)
        {
            if (!PageEditing.PageIsInEditMode)
            {
                if (PageContext?.Page != null && PageContext.Page.StopPublish <= datetime.now) { if(pagecontext.page.pagetypename="=" "specialistpage") { icontentrepository cr="EPiServer.ServiceLocation.ServiceLocator.Current.GetInstance">();
                        SiteSettingsPage childPages = cr.GetChildren(ContentReference.StartPage).FirstOrDefault();
                        IContentRepository factory = DataFactory.Instance;
                        UitDienstPage specialistVerlopenPage = factory.Get(childPages.SpecialistVerlopenPage);

                        if(specialistVerlopenPage != null)
                        {
							//Here is where I'm trying to do the magic, but this is not working... ¬¬
                            filterContext.Result =
                                new RedirectToRouteResult(
                                    new RouteValueDictionary(
                                        new {action = "SpecialistVerlopen",
                                             model = specialistVerlopenPage}));

                            return;
                        }
                    }

                    filterContext.Result = new HttpStatusCodeResult(404, "Not found");
                    return;
                }
            }

            base.OnAuthorization(filterContext);
        }

        public ActionResult SpecialistVerlopen(UitDienstPage specialistVerlopenPage)
        {
            var viewModel = new UitDienstPageViewModel(specialistVerlopenPage);
            return View("~/Views/Pages/UitDienstPagePage.cshtml", viewModel);
        }

I don't know what I'm doing wrong.

Could anyone explain me how to do that and why my code is not working properly?.

Best regards.

#191065
Apr 19, 2018 15:43
Vote:
 

I don't think model is a valid dictionary value. It expects a controller and action.

Can you try something like this?

new RedirectToAction("ControllerName","ActionName", model);
#191088
Apr 19, 2018 21:22
Vote:
 

simple `RedirectToAction` probably might not work. See this question on SO

#191096
Apr 20, 2018 5:09
Vote:
 

One option could be to do the redirection earlier during routing (instead of in an action filter). You could for example attach an event handler for EPiServer.Web.Routing.IContentRouteEvents.RoutedContent. The event argument has a property RoutingSegmentContext which have a RoutedContentLink property that specifies which content that the request have been routed to.

So what you could do is to check if that content has expired and if so call method PermanentRedirect() (or TemporaryRedirect) that are methods on RoutingSegmentContext property on event args.

#191111
Apr 20, 2018 9:02
Vote:
 

Hello again everyone.

Finally it works, thanks to all for your support, was very helpful, this is what I did to do it works:

On my PageControllerBase (or BaseController) I did the Follow:

        protected override void OnAuthorization(AuthorizationContext filterContext)
        {
            // don't throw 404 in edit mode
            if (!PageEditing.PageIsInEditMode)
            {
                if (PageContext?.Page != null && PageContext.Page.StopPublish <= DateTime.Now)
                {
                    if (PageContext.Page.PageTypeName == "SpecialistPage")
                    {
                        IContentRepository cr = EPiServer.ServiceLocation.ServiceLocator.Current.GetInstance<IContentRepository>();
                        SiteSettingsPage childPages = cr.GetChildren<SiteSettingsPage>(ContentReference.StartPage).FirstOrDefault();
                        IContentRepository factory = DataFactory.Instance;
                        UitDienstPage specialistVerlopenPage = factory.Get<UitDienstPage>(childPages.SpecialistVerlopenPage);

                        if (specialistVerlopenPage != null)
                        {
                            filterContext.Result = this.RedirectToAction("SpecialistVerlopen", "UitDienstPage", specialistVerlopenPage);
                            return;
                        }
                    }
                    else
                    {
                        filterContext.Result = new HttpStatusCodeResult(404, "Not found");
                        return;
                    }
                }
                else
                {
                    //If the page is not expired, then go ahead...
                    base.OnAuthorization(filterContext);
                }
            }
            else
            {
                base.OnAuthorization(filterContext);
            }
        }

        private new RedirectToRouteResult RedirectToAction(string action, string controller, UitDienstPage model)
        {
            TempData["TempSpecialistVerlopenPage"] = model;
            return base.RedirectToAction(action, controller, new { node = model.ContentLink });
        }

And also I have created a new Controller to call and render my VewModel:

    public class UitDienstPageController : PageControllerBase<UitDienstPage>
    {
        // GET: UitDienstPage
        public ViewResult SpecialistVerlopen()
        {
            UitDienstPage model = (UitDienstPage)TempData["TempSpecialistVerlopenPage"];
            var viewModel = new UitDienstPageViewModel(model);
            return View("~/Views/Pages/UitDienstPage.cshtml", viewModel);
        }
    }

This have worked for me.

Do you see guys another way to accomplish this?

or

Do you see anything weird/strange on my code?

Best regards.

#191975
May 03, 2018 12:11
Vote:
 

Hi,

finally I made a new change that looks more clean and efficiently.

On my PageControllerBase (or BaseController) I did the Follow:

        protected override void OnAuthorization(AuthorizationContext filterContext)
        {
            // don't throw 404 in edit mode
            if (!PageEditing.PageIsInEditMode)
            {
                if (PageContext?.Page != null && PageContext.Page.StopPublish <= DateTime.Now)
                {
                    if (PageContext.Page.PageTypeName == "SpecialistPage")
                    {
                        IContentRepository cr = EPiServer.ServiceLocation.ServiceLocator.Current.GetInstance<IContentRepository>();
                        SiteSettingsPage childPages = cr.GetChildren<SiteSettingsPage>(ContentReference.StartPage).FirstOrDefault();
                        IContentRepository factory = DataFactory.Instance;
                        UitDienstPage specialistVerlopenPage = factory.Get<UitDienstPage>(childPages.SpecialistVerlopenPage);

                        if (specialistVerlopenPage != null)
                        {
                            filterContext.Result = Redirect(UrlResolver.Current.GetUrl(specialistVerlopenPage.ContentLink));
                            return;
                        }
                    }
                    else
                    {
                        filterContext.Result = new HttpStatusCodeResult(404, "Not found");
                        return;
                    }
                }
                else
                {
                    //If the page is not expired, then go ahead...
                    base.OnAuthorization(filterContext);
                }
            }
            else
            {
                base.OnAuthorization(filterContext);
            }
        }

An also I have removed the method and the new controller "UitDienstPageController" because I don't going to us it anymore.

       private new RedirectToRouteResult RedirectToAction(string action, string controller, UitDienstPage model)
        {
            TempData["TempSpecialistVerlopenPage"] = model;
            return base.RedirectToAction(action, controller, new { node = model.ContentLink });
        }

I hope this post could help to more people with the same problem.

Thank you everyone for your help and support.

Best regards.

#192058
May 04, 2018 11:47
This topic was created over six months ago and has been resolved. If you have a similar question, please create a new topic and refer to this one.
* You are NOT allowed to include any hyperlinks in the post because your account hasn't associated to your company. User profile should be updated.