This project is read-only.

Redirect from page based on custom rules

Topics: Customizing Orchard
Jul 27, 2011 at 2:49 PM

I have added some logic into an ActionFilter to redirect from one page to another, but am wondering if there is a better way to target a page without just looking at the RouteData. My current filter is doing something like this:


public void OnActionExecuting(ActionExecutingContext filterContext)
            var path = filterContext.RouteData.Values["path"] as string ?? string.Empty;
            var area = filterContext.RouteData.Values["area"] as string ?? string.Empty;

            if (path.Equals("targetpage")
                && area.Equals("Routable"))


I am basically looking to replace the if condition with something a bit more robust. Can I easily access the current content item and look at a particular property that won't change?

I imagine Orchard might not even know what content item is being requested at this point as the filter will be running before the the ItemController's Display action. If so can anybody suggest a better way of achieving this redirect? If it helps at all, what I am verifying is that an ID stored on the query string is present and valid (based on some custom rules) for a particular page.

Jul 27, 2011 at 9:21 PM

How is this not robust? Sounds fine to me.

Jul 28, 2011 at 9:06 AM

I'm mainly concerned that if we change the path of the page then the code also needs to be changed.

Jul 29, 2011 at 1:43 AM

Of course, but how else than with the path are you going to identify a specific page? Using the id would be a lot less robust, I think, as it's a technical value that you shouldn't hard-code. The path should be a stable thing as much as possible, for many reasons, one being SEO.

Jul 29, 2011 at 9:06 AM

My question was, is there another way, to which the answer seems to be no. However, in this specific case I decided a custom layer rule better fitted the requirement as I can have one page that uses different layers based on my logic, rather than relying on a redirect.