Click here to Skip to main content
15,880,392 members
Articles / Hosted Services / Azure

YouConf - Your Live Online Conferencing Tool

Rate me:
Please Sign up or sign in to vote.
4.96/5 (41 votes)
17 Nov 2013CPOL167 min read 258.2K   660   70  
A site for managing and delivering virtual conferences - complete with embedded live streaming and chat. Showcasing the best Azure has to offer!
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Web;
using System.Web.Mvc;
using System.Web.Routing;

namespace YouConf
{
    public class RouteConfig
    {
        public static void RegisterRoutes(RouteCollection routes)
        {
            routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

            routes.MapRoute(
                name: "ConferenceFriendlyUrl",
                url: "{hashTag}/{action}",
                defaults: new { controller = "Conference", action = "Details" },
                constraints: new { hashTag = new IsNotAControllerNameConstraint() }
            );

            routes.MapRoute(
                name: "Default",
                url: "{controller}/{action}/{id}",
                defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
            );
        }
    }


    /// <summary>
    /// Thanks to http://stackoverflow.com/questions/10407365/mvc-routing-constraint-on-controller-names for the idea behind this.
    /// Modified the code to cache the list of controller names and enhance lookup speed using a HashSet rather than List
    /// </summary>
    public class IsNotAControllerNameConstraint : IRouteConstraint
    {
        private static HashSet<string> ControllerNames = GetControllerNames();

        private static List<Type> GetSubClasses<T>()
        {
            return Assembly.GetCallingAssembly().GetTypes().Where(
                type => type.IsSubclassOf(typeof(T))).ToList();
        }

        public static HashSet<string> GetControllerNames()
        {
            var controllerNames = new HashSet<string>(StringComparer.InvariantCultureIgnoreCase);
            GetSubClasses<Controller>().ForEach(
                type => controllerNames.Add(type.Name));
            return controllerNames;
        }
        public bool Match(HttpContextBase httpContext, Route route, string parameterName, RouteValueDictionary values, RouteDirection routeDirection)
        {
            if (values.ContainsKey(parameterName))
            {
                string stringValue = values[parameterName] as string;
                return !ControllerNames.Contains(stringValue + "Controller");
            }

            return true;
        }
    }
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer (Senior)
New Zealand New Zealand
I'm a software developer based in the beautiful city of Wellington, New Zealand. I love experimenting with new .Net technologies as they arrive, and these days there seems to be a lot of choice as there are so many new features in the framework! My current interests are Azure, ASP.Net MVC, SignalR, Knockout, AngularJS, and responsive design (inc. using Bootstrap, Foundation, Skeleton). These change fairly often as I tinker with various new technologies...

Comments and Discussions