// 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 System.IO; using Microsoft.AspNetCore.Rewrite.Internal.IISUrlRewrite; using Microsoft.Extensions.FileProviders; namespace Microsoft.AspNetCore.Rewrite { /// /// Extensions for adding IIS Url Rewrite rules to /// public static class IISUrlRewriteOptionsExtensions { /// /// Add rules from a IIS config file containing Url Rewrite rules /// /// The /// The /// The path to the file containing UrlRewrite rules. public static RewriteOptions AddIISUrlRewrite(this RewriteOptions options, IFileProvider fileProvider, string filePath) { if (options == null) { throw new ArgumentNullException(nameof(options)); } if (fileProvider == null) { throw new ArgumentNullException(nameof(fileProvider)); } var file = fileProvider.GetFileInfo(filePath); using (var stream = file.CreateReadStream()) { return AddIISUrlRewrite(options, new StreamReader(stream)); } } /// /// Add rules from a IIS config file containing Url Rewrite rules /// /// The /// The text reader stream. public static RewriteOptions AddIISUrlRewrite(this RewriteOptions options, TextReader reader) { if (options == null) { throw new ArgumentNullException(nameof(options)); } if (reader == null) { throw new ArgumentException(nameof(reader)); } var rules = new UrlRewriteFileParser().Parse(reader); foreach (var rule in rules) { options.Rules.Add(rule); } return options; } } }