This repository has been archived by the owner on Dec 20, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 84
/
IISUrlRewriteOptionsExtensions.cs
69 lines (59 loc) · 2.32 KB
/
IISUrlRewriteOptionsExtensions.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
// 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
{
/// <summary>
/// Extensions for adding IIS Url Rewrite rules to <see cref="RewriteOptions"/>
/// </summary>
public static class IISUrlRewriteOptionsExtensions
{
/// <summary>
/// Add rules from a IIS config file containing Url Rewrite rules
/// </summary>
/// <param name="options">The <see cref="RewriteOptions"/></param>
/// <param name="fileProvider">The <see cref="IFileProvider"/> </param>
/// <param name="filePath">The path to the file containing UrlRewrite rules.</param>
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));
}
}
/// <summary>
/// Add rules from a IIS config file containing Url Rewrite rules
/// </summary>
/// <param name="options">The <see cref="RewriteOptions"/></param>
/// <param name="reader">The text reader stream.</param>
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;
}
}
}