/// <summary> /// Add mapped routes. /// </summary> public void MapRoute(string name, string regex, DefaultRoute defaults) { MappedRoute mappedRoute = new MappedRoute(); mappedRoute.name = name; mappedRoute.regex = regex; mappedRoute.defaults = defaults; _mappedRoutes.Add(mappedRoute); }
/// <summary> /// Add mapped routes. /// </summary> public void MapRoute(string name, string regex, DefaultRoute defaults) { MappedRoute mappedRoute = new MappedRoute(); mappedRoute.name = name; mappedRoute.regex = regex; mappedRoute.defaults = defaults; _mappedRoutes.Add(mappedRoute); }
/// <summary> /// Route the request. /// </summary> /// <param name="context">HTTP context</param> /// <returns><see cref="ModuleResult.Stop"/> will stop all processing including <see cref="IHttpModule.EndRequest"/>.</returns> /// <remarks>Simply change the request URI to something else.</remarks> public ModuleResult Route(IHttpContext context) { MatchResult results = _routes.Match(context); if (results != null) { if (results.MatchStatus.Success) { DefaultRoute defaultRoute = results.MappedRoute.defaults; string path = String.Empty; if (!StringUtility.IsNullOrEmpty(defaultRoute.controller)) { path += string.Concat("/", defaultRoute.controller); } if (!StringUtility.IsNullOrEmpty(defaultRoute.action)) { path += string.Concat("/", defaultRoute.action); } if (!StringUtility.IsNullOrEmpty(defaultRoute.id)) { path += string.Concat("?id=", defaultRoute.id); } if (StringUtility.IsNullOrEmpty(path)) { path = context.Request.Uri.AbsolutePath; } Uri url = context.Request.Uri; string port = string.Empty; if (url.Port != 80) { port = string.Concat(";", url.Port); } context.Request.UriRewrite = new Uri( string.Concat(url.Scheme, "://", url.Host, port, path)); } return(ModuleResult.Continue); } context.Response.StatusCode = 403; context.Response.StatusDescription = "Forbidden"; return(ModuleResult.Stop); }
/// <summary> /// Log the loaded routes. /// </summary> private void LogRoutes() { Logger.WriteInfo(this, "Ignored routes loaded from RouteConfig (HttpApplication) class:"); foreach (string pattern in _routes.IngoredRoutes) { Logger.WriteInfo(" route regex: '" + pattern + "'"); } Logger.WriteInfo(this, "Mapped routes loaded from RouteConfig (HttpApplication) class:"); foreach (MappedRoute items in _routes.MappedRoutes) { MappedRoute item = items; DefaultRoute route = item.defaults; Logger.WriteInfo(" route name: '" + item.name + "' regex: '" + item.regex + "' => default: {controller='" + route.controller + "' action='" + route.action + "' id='" + route.id + "'}" ); } }