Change the IHostingEnvironment parameter to IFileProvider for top-level IIS and Apache extension methods

Also fixup some xmldocs
This commit is contained in:
Nate McMaster 2016-09-06 11:07:41 -07:00
parent d2913edf61
commit d2fb2cd776
No known key found for this signature in database
GPG Key ID: BD729980AA6A21BD
9 changed files with 45 additions and 52 deletions

View File

@ -11,14 +11,14 @@ namespace RewriteSample
{
public class Startup
{
public void Configure(IApplicationBuilder app, IHostingEnvironment hostingEnvironment)
public void Configure(IApplicationBuilder app, IHostingEnvironment env)
{
var options = new RewriteOptions()
.AddRedirect("(.*)/$", "$1")
.AddRewrite(@"app/(\d+)", "app?id=$1")
.AddRedirectToHttps(302)
.AddIISUrlRewrite(hostingEnvironment, "UrlRewrite.xml")
.AddApacheModRewrite(hostingEnvironment, "Rewrite.txt");
.AddIISUrlRewrite(env.ContentRootFileProvider, "UrlRewrite.xml")
.AddApacheModRewrite(env.ContentRootFileProvider, "Rewrite.txt");
app.UseRewriter(options);

View File

@ -3,50 +3,45 @@
using System;
using System.IO;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Rewrite.Internal.ApacheModRewrite;
using Microsoft.Extensions.FileProviders;
namespace Microsoft.AspNetCore.Rewrite
{
/// <summary>
/// Apache mod_rewrite extensions on top of the <see cref="RewriteOptions"/>
/// Extensions for adding Apache mod_rewrite rules to <see cref="RewriteOptions"/>
/// </summary>
public static class ApacheModRewriteOptionsExtensions
{
/// <summary>
/// Imports rules from a mod_rewrite file and adds the rules to current rules.
/// Add rules from an Apache mod_rewrite file
/// </summary>
/// <param name="options">The Rewrite options.</param>
/// <param name="hostingEnvironment">The Hosting Environment</param>
/// <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 mod_rewrite rules.</param>
public static RewriteOptions AddApacheModRewrite(this RewriteOptions options, IHostingEnvironment hostingEnvironment, string filePath)
public static RewriteOptions AddApacheModRewrite(this RewriteOptions options, IFileProvider fileProvider, string filePath)
{
if (options == null)
{
throw new ArgumentNullException(nameof(options));
}
if (hostingEnvironment == null)
if (fileProvider == null)
{
throw new ArgumentNullException(nameof(hostingEnvironment));
throw new ArgumentNullException(nameof(fileProvider));
}
if (string.IsNullOrEmpty(filePath))
{
throw new ArgumentException(nameof(filePath));
}
var path = Path.Combine(hostingEnvironment.ContentRootPath, filePath);
using (var stream = File.OpenRead(path))
var fileInfo = fileProvider.GetFileInfo(filePath);
using (var stream = fileInfo.CreateReadStream())
{
return options.AddApacheModRewrite(new StreamReader(stream));
}
}
/// <summary>
/// Imports rules from a mod_rewrite file and adds the rules to current rules.
/// Add rules from an Apache mod_rewrite file
/// </summary>
/// <param name="options">The Rewrite options.</param>
/// <param name="options">The <see cref="RewriteOptions"/></param>
/// <param name="reader">A stream of mod_rewrite rules.</param>
public static RewriteOptions AddApacheModRewrite(this RewriteOptions options, TextReader reader)
{

View File

@ -3,50 +3,46 @@
using System;
using System.IO;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Rewrite.Internal.IISUrlRewrite;
using Microsoft.Extensions.FileProviders;
namespace Microsoft.AspNetCore.Rewrite
{
/// <summary>
/// IIS Url rewrite extensions on top of the <see cref="RewriteOptions"/>
/// Extensions for adding IIS Url Rewrite rules to <see cref="RewriteOptions"/>
/// </summary>
public static class IISUrlRewriteOptionsExtensions
{
/// <summary>
/// Imports rules from a mod_rewrite file and adds the rules to current rules.
/// Add rules from a IIS config file containing Url Rewrite rules
/// </summary>
/// <param name="options">The UrlRewrite options.</param>
/// <param name="hostingEnvironment"></param>
/// <param name="filePath">The path to the file containing urlrewrite rules.</param>
public static RewriteOptions AddIISUrlRewrite(this RewriteOptions options, IHostingEnvironment hostingEnvironment, string filePath)
/// <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 (hostingEnvironment == null)
if (fileProvider == null)
{
throw new ArgumentNullException(nameof(hostingEnvironment));
throw new ArgumentNullException(nameof(fileProvider));
}
if (string.IsNullOrEmpty(filePath))
{
throw new ArgumentException(nameof(filePath));
}
var file = fileProvider.GetFileInfo(filePath);
var path = Path.Combine(hostingEnvironment.ContentRootPath, filePath);
using (var stream = File.OpenRead(path))
using (var stream = file.CreateReadStream())
{
return AddIISUrlRewrite(options, new StreamReader(stream));
}
}
/// <summary>
/// Imports rules from a mod_rewrite file and adds the rules to current rules.
/// Add rules from a IIS config file containing Url Rewrite rules
/// </summary>
/// <param name="options">The UrlRewrite options.</param>
/// <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)
{
@ -66,6 +62,7 @@ namespace Microsoft.AspNetCore.Rewrite
{
options.Rules.Add(rule);
}
return options;
}
}

View File

@ -14,7 +14,7 @@ namespace Microsoft.AspNetCore.Builder
/// <summary>
/// Checks if a given Url matches rules and conditions, and modifies the HttpContext on match.
/// </summary>
/// <param name="app"></param>
/// <param name="app">The <see cref="IApplicationBuilder"/></param>
/// <param name="options">Options for rewrite.</param>
/// <returns></returns>
public static IApplicationBuilder UseRewriter(this IApplicationBuilder app, RewriteOptions options)

View File

@ -14,7 +14,7 @@ using Microsoft.Net.Http.Headers;
namespace Microsoft.AspNetCore.Rewrite
{
/// <summary>
/// Represents a middleware that rewrites urls imported from mod_rewrite, UrlRewrite, and code.
/// Represents a middleware that rewrites urls
/// </summary>
public class RewriteMiddleware
{

View File

@ -3,6 +3,7 @@
using System.Collections.Generic;
using Microsoft.Extensions.FileProviders;
using Microsoft.AspNetCore.Hosting;
namespace Microsoft.AspNetCore.Rewrite
{
@ -17,7 +18,7 @@ namespace Microsoft.AspNetCore.Rewrite
public IList<Rule> Rules { get; } = new List<Rule>();
/// <summary>
/// Gets and sets the File Provider for file and directory checks.
/// Gets and sets the File Provider for file and directory checks. Defaults to <see cref="IHostingEnvironment.WebRootFileProvider"/>
/// </summary>
public IFileProvider StaticFileProvider { get; set; }
}

View File

@ -14,7 +14,7 @@ namespace Microsoft.AspNetCore.Rewrite
/// <summary>
/// Adds a rule to the current rules.
/// </summary>
/// <param name="options">The UrlRewrite options.</param>
/// <param name="options">The <see cref="RewriteOptions"/>.</param>
/// <param name="rule">A rule to be added to the current rules.</param>
/// <returns>The Rewrite options.</returns>
public static RewriteOptions Add(this RewriteOptions options, Rule rule)
@ -26,7 +26,7 @@ namespace Microsoft.AspNetCore.Rewrite
/// <summary>
/// Adds a rule to the current rules.
/// </summary>
/// <param name="options">The Rewrite options.</param>
/// <param name="options">The <see cref="RewriteOptions"/>.</param>
/// <param name="applyRule">A Func that checks and applies the rule.</param>
/// <returns></returns>
public static RewriteOptions Add(this RewriteOptions options, Action<RewriteContext> applyRule)
@ -38,7 +38,7 @@ namespace Microsoft.AspNetCore.Rewrite
/// <summary>
/// Rewrites the path if the regex matches the HttpContext's PathString
/// </summary>
/// <param name="options">The Rewrite options.</param>
/// <param name="options">The <see cref="RewriteOptions"/>.</param>
/// <param name="regex">The regex string to compare with.</param>
/// <param name="replacement">If the regex matches, what to replace HttpContext with.</param>
/// <returns>The Rewrite options.</returns>
@ -50,7 +50,7 @@ namespace Microsoft.AspNetCore.Rewrite
/// <summary>
/// Rewrites the path if the regex matches the HttpContext's PathString
/// </summary>
/// <param name="options">The Rewrite options.</param>
/// <param name="options">The <see cref="RewriteOptions"/>.</param>
/// <param name="regex">The regex string to compare with.</param>
/// <param name="replacement">If the regex matches, what to replace the uri with.</param>
/// <param name="stopProcessing">If the regex matches, conditionally stop processing other rules.</param>
@ -64,7 +64,7 @@ namespace Microsoft.AspNetCore.Rewrite
/// <summary>
/// Redirect the request if the regex matches the HttpContext's PathString
/// </summary>
/// <param name="options">The Rewrite options.</param>
/// <param name="options">The <see cref="RewriteOptions"/>.</param>
/// <param name="regex">The regex string to compare with.</param>
/// <param name="replacement">If the regex matches, what to replace the uri with.</param>
/// <returns>The Rewrite options.</returns>
@ -76,7 +76,7 @@ namespace Microsoft.AspNetCore.Rewrite
/// <summary>
/// Redirect the request if the regex matches the HttpContext's PathString
/// </summary>
/// <param name="options">The Rewrite options.</param>
/// <param name="options">The <see cref="RewriteOptions"/>.</param>
/// <param name="regex">The regex string to compare with.</param>
/// <param name="replacement">If the regex matches, what to replace the uri with.</param>
/// <param name="statusCode">The status code to add to the response.</param>
@ -91,7 +91,7 @@ namespace Microsoft.AspNetCore.Rewrite
/// Redirect a request to https if the incoming request is http, with returning a 301
/// status code for permanently redirected.
/// </summary>
/// <param name="options"></param>
/// <param name="options">The <see cref="RewriteOptions"/>.</param>
/// <returns></returns>
public static RewriteOptions AddRedirectToHttpsPermanent(this RewriteOptions options)
{
@ -101,7 +101,7 @@ namespace Microsoft.AspNetCore.Rewrite
/// <summary>
/// Redirect a request to https if the incoming request is http
/// </summary>
/// <param name="options">The Rewrite options.</param>
/// <param name="options">The <see cref="RewriteOptions"/>.</param>
public static RewriteOptions AddRedirectToHttps(this RewriteOptions options)
{
return AddRedirectToHttps(options, statusCode: 302, sslPort: null);
@ -110,7 +110,7 @@ namespace Microsoft.AspNetCore.Rewrite
/// <summary>
/// Redirect a request to https if the incoming request is http
/// </summary>
/// <param name="options">The Rewrite options.</param>
/// <param name="options">The <see cref="RewriteOptions"/>.</param>
/// <param name="statusCode">The status code to add to the response.</param>
public static RewriteOptions AddRedirectToHttps(this RewriteOptions options, int statusCode)
{
@ -120,7 +120,7 @@ namespace Microsoft.AspNetCore.Rewrite
/// <summary>
/// Redirect a request to https if the incoming request is http
/// </summary>
/// <param name="options">The Rewrite options.</param>
/// <param name="options">The <see cref="RewriteOptions"/>.</param>
/// <param name="statusCode">The status code to add to the response.</param>
/// <param name="sslPort">The SSL port to add to the response.</param>
public static RewriteOptions AddRedirectToHttps(this RewriteOptions options, int statusCode, int? sslPort)

View File

@ -10,8 +10,8 @@ namespace Microsoft.AspNetCore.Rewrite
{
/// <summary>
/// Applies the rule.
/// Implementations of ApplyRule should set the value for RewriteContext.Results
/// (defaults to RuleTermination.Continue)
/// Implementations of ApplyRule should set the value for <see cref="RewriteContext.Result"/>
/// (defaults to <see cref="RuleTermination.Continue"/> )
/// </summary>
/// <param name="context"></param>
public abstract void ApplyRule(RewriteContext context);