This project is read-only.

Creating Routes in Orchard

Topics: Core
Jan 16, 2015 at 7:39 PM
I'm trying to create a cookie that will contain the name of a random image as soon as the user hits my Orchard site by using a custom route and controller. My Routes.cs file is as follows:

namespace Orchard.DynamicBanners
public class Routes : IRouteProvider
    public void GetRoutes(ICollection<RouteDescriptor> routes)
        foreach (var routeDescriptor in GetRoutes())

    public IEnumerable<RouteDescriptor> GetRoutes()
        return new[] {
            new RouteDescriptor {
                Priority = 15,
                Route = new Route(
                    new RouteValueDictionary {
                        {"area", "Orchard.DynamicBanners"},
                        {"controller", "Banner"},
                        {"action", "SelectBanner"}
                    new RouteValueDictionary(),
                    new RouteValueDictionary {
                        {"area", "Orchard.DynamicBanners"}
                    new MvcRouteHandler())

If I use "test" as my route then it will succesfully create the cookie when I enter "mysite/test" into the url and browse to it. I need to somehow specify this route for all pages within my Orchard site so as soon as a page is hit the cookie will be created. I want an image that changes every time a new session is started or every twenty minutes (I can specify this in the expiration of the cookie) I just need a way for my cookie to be created as soon as any page on the site is hit. If there is a better way of going about this I am open to suggestions, thank you.
Jan 16, 2015 at 7:48 PM
It appears this will not actually work as it overrides any routes that were previously specified instead of running both routes. How can I create a cookie as soon as any page on my site has been hit?
Jan 16, 2015 at 8:07 PM
Is there a way to redirect back to the route that was specified after hitting my custom route? Say if /admin is specified then it will hit my controller and action for all routes and then go to the controller for the /admin route?
Jan 17, 2015 at 10:29 AM
No. I mean yes, but that would require you looking through the whole routing table. Kinda overkill when there is simpler solution.
What you actually need is an action filter that will do something when user enters /admin. No need to override any routes.

Technically speaking, action filter is just one class implementing both FilterProvider and IActionFilter. There are lots of examples throughout Orchard code.
Be aware that the filter will be fired for each and every request so you need to check if the URL matches your requirements.
Jan 17, 2015 at 11:16 PM
Thank you pszmyd for replying. So I should be able to create an action filter that will run my code to create a cookie with the name of the image and then pull from that cookie in the code that will display the image in the banner? This filter will run the code on every page request so I could just make sure it doesn't write over the cookie if it already exists? How would I specify the filter to run based on the URL? I'm thinking I only need the filter to run on the front end so any URL that has "/admin" could be ignored. Thank you again.
Jan 18, 2015 at 11:53 AM
Edited Jan 18, 2015 at 11:53 AM
Filter allows you to specify a piece of code that runs before or after the controller action. So yes, you can create a cookie there. You can access all information about the request, alter the response etc. via the ActionExecutingContext/ActionExecutedContext object passed to the filter.

If you need to ignore all admin pages, there is a shorthand for this already - just check AdminFilter.IsApplied(filterContext.RequestContext).