/// <summary> /// Gets a rule matching the given request URI. /// </summary> /// <param name="requestUri">The request URI to get a matching rule for.</param> /// <param name="rules">The rule collection to use when matching.</param> /// <returns>A matching rule, or null if none was found.</returns> public virtual HttpRedirectRuleMatch Match(Uri requestUri, IEnumerable<HttpRedirectRuleElement> rules) { if (requestUri == null) { throw new ArgumentNullException("requestUri", "requestUrl cannot be null."); } HttpRedirectRuleMatch ruleMatch = null; string uriValue = requestUri.ToString(); if (rules != null) { foreach (var rule in rules) { Match match = Regex.Match(uriValue, rule.Pattern, RegexOptions.IgnoreCase); if (match.Success) { ruleMatch = new HttpRedirectRuleMatch(rule, match); break; } } } return ruleMatch; }
/// <summary> /// Redirects the given HTTP context to the redirect result of the given rule match. /// </summary> /// <param name="httpContext">The HTTP context to redirect.</param> /// <param name="ruleMatch">The rule match to redirect with.</param> public virtual void RedirectContext(HttpContextBase httpContext, HttpRedirectRuleMatch ruleMatch) { if (httpContext == null) { throw new ArgumentNullException("httpContext", "httpContext cannot be null."); } if (ruleMatch == null) { throw new ArgumentNullException("ruleMatch", "rule cannot be null."); } if (!String.IsNullOrEmpty(ruleMatch.RedirectResult)) { httpContext.Response.Clear(); httpContext.Response.StatusCode = ruleMatch.Rule.RedirectType == HttpRedirectType.Permanent ? 301 : 302; httpContext.Response.AddHeader("Location", ruleMatch.RedirectResult); httpContext.Response.End(); } }