// Copyright (c) .NET Foundation. All rights reserved. // Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. using System; using Microsoft.AspNetCore.Rewrite.Internal; namespace Microsoft.AspNetCore.Rewrite { /// /// The builder to a list of rules for and /// public static class RewriteOptionsExtensions { /// /// Adds a rule to the current rules. /// /// The . /// A rule to be added to the current rules. /// The Rewrite options. public static RewriteOptions Add(this RewriteOptions options, Rule rule) { options.Rules.Add(rule); return options; } /// /// Adds a rule to the current rules. /// /// The . /// A Func that checks and applies the rule. /// public static RewriteOptions Add(this RewriteOptions options, Action applyRule) { options.Rules.Add(new DelegateRule(applyRule)); return options; } /// /// Rewrites the path if the regex matches the HttpContext's PathString /// /// The . /// The regex string to compare with. /// If the regex matches, what to replace HttpContext with. /// The Rewrite options. public static RewriteOptions AddRewrite(this RewriteOptions options, string regex, string replacement) { return AddRewrite(options, regex, replacement, stopProcessing: false); } /// /// Rewrites the path if the regex matches the HttpContext's PathString /// /// The . /// The regex string to compare with. /// If the regex matches, what to replace the uri with. /// If the regex matches, conditionally stop processing other rules. /// The Rewrite options. public static RewriteOptions AddRewrite(this RewriteOptions options, string regex, string replacement, bool stopProcessing) { options.Rules.Add(new RewriteRule(regex, replacement, stopProcessing)); return options; } /// /// Redirect the request if the regex matches the HttpContext's PathString /// /// The . /// The regex string to compare with. /// If the regex matches, what to replace the uri with. /// The Rewrite options. public static RewriteOptions AddRedirect(this RewriteOptions options, string regex, string replacement) { return AddRedirect(options, regex, replacement, statusCode: 302); } /// /// Redirect the request if the regex matches the HttpContext's PathString /// /// The . /// The regex string to compare with. /// If the regex matches, what to replace the uri with. /// The status code to add to the response. /// The Rewrite options. public static RewriteOptions AddRedirect(this RewriteOptions options, string regex, string replacement, int statusCode) { options.Rules.Add(new RedirectRule(regex, replacement, statusCode)); return options; } /// /// Redirect a request to https if the incoming request is http, with returning a 301 /// status code for permanently redirected. /// /// The . /// public static RewriteOptions AddRedirectToHttpsPermanent(this RewriteOptions options) { return AddRedirectToHttps(options, statusCode: 301, sslPort: null); } /// /// Redirect a request to https if the incoming request is http /// /// The . public static RewriteOptions AddRedirectToHttps(this RewriteOptions options) { return AddRedirectToHttps(options, statusCode: 302, sslPort: null); } /// /// Redirect a request to https if the incoming request is http /// /// The . /// The status code to add to the response. public static RewriteOptions AddRedirectToHttps(this RewriteOptions options, int statusCode) { return AddRedirectToHttps(options, statusCode, sslPort: null); } /// /// Redirect a request to https if the incoming request is http /// /// The . /// The status code to add to the response. /// The SSL port to add to the response. public static RewriteOptions AddRedirectToHttps(this RewriteOptions options, int statusCode, int? sslPort) { options.Rules.Add(new RedirectToHttpsRule { StatusCode = statusCode, SSLPort = sslPort }); return options; } } }