using System.IO; using System.Text; using System.Web; using System.Web.Mvc; using System.Web.Routing; namespace Website { public class ApplicationRoutes { private readonly RouteCollection routes; public ApplicationRoutes(RouteCollection routes) { this.routes = routes; } public void Register() { routes.IgnoreRoute("{resource}.axd/{*pathInfo}"); routes.IgnoreRoute("{config}.config/{*pathInfo}"); routes.MapRoute("Content Pages", "{pageName}", new { controller = "Home", action = "ContentPage", pageName = "Home" }, new { pageName = getHomeConstraints() } ); routes.MapRoute("Static Content", "{action}/{*path}", new { controller = "StaticContent" }, new { action = "(scripts|styles)" } ); routes.MapRoute("Redirects (by code)", "go/{*code}", new { controller = "Redirects", action = "RedirectByCode" } ); routes.MapRoute("Error Page", "error/{statusCode}", new { controller = "Error", action = "Index", statusCode = "General" } ); routes.MapRoute("JSON Requests", "{pageName}/{renderMode}", new { controller = "Home", action = "ContentPage", pageName = "Index" }, new { renderMode = "(json|xml|partial)" } ); // Send any old links (with the .mvc extension) to the RetroContent action routes.MapRoute("Retro", "{pageName}.mvc", new { controller = "Home", action = "RetroContent" } ); // NOTE: I've explicitly removed the catch-all route and replaced it with my redirect controller routes.MapRoute("Redirects (by path)", "{*path}", new { controller = "Redirects", action = "RedirectByPath" }, new { path = "(downloads|demos).*" } ); } private static string getHomeConstraints() { var views = new StringBuilder("(home"); // Cycle through the /Views/Home directory and add all of the views therein // to the list of accepted view names var viewsDirectory = new DirectoryInfo(HttpContext.Current.Server.MapPath("~/Views/Home")); foreach (var view in viewsDirectory.GetFiles("*.aspx")) views.AppendFormat("|{0}", view.Name.Substring(0, view.Name.LastIndexOf(".aspx"))); views.Append(")"); return views.ToString(); } } }