AspNetCore.Docs/aspnetcore/fundamentals/url-rewriting/sample/RewriteRule.cs

59 lines
2.1 KiB
C#
Raw Normal View History

2017-01-11 22:50:32 +08:00
// 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.Text.RegularExpressions;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Rewrite;
using Microsoft.Net.Http.Headers;
namespace RewriteRules
{
#region snippet1
public class RedirectImageRequests : IRule
{
private readonly string _extension;
private readonly PathString _newPath;
public RedirectImageRequests(string extension, string newPath)
{
if (string.IsNullOrEmpty(extension))
{
throw new ArgumentException(nameof(extension));
}
if (!Regex.IsMatch(extension, @"^\.(png|jpg|gif)$"))
{
throw new ArgumentException("The extension is not valid. The extension must be .png, .jpg, or .gif.", nameof(extension));
}
if (!Regex.IsMatch(newPath, @"(/[A-Za-z0-9]+)+?"))
{
throw new ArgumentException("The path is not valid. Provide an alphanumeric path that starts with a forward slash.", nameof(newPath));
}
_extension = extension;
_newPath = new PathString(newPath);
}
public void ApplyRule(RewriteContext context)
{
var request = context.HttpContext.Request;
// Because we're redirecting back to the same app, stop processing if the request has already been redirected
if (request.Path.StartsWithSegments(new PathString(_newPath)))
{
return;
}
if (request.Path.Value.EndsWith(_extension, StringComparison.OrdinalIgnoreCase))
{
var response = context.HttpContext.Response;
response.StatusCode = StatusCodes.Status301MovedPermanently;
context.Result = RuleResult.EndResponse;
response.Headers[HeaderNames.Location] = _newPath + request.Path + request.QueryString;
}
}
}
#endregion
URL Rewriting doc (#2446) * Update url-rewriting.md * Create project.json * Add files via upload * Create add_rewrite.png * Add files via upload * Update toc.md * Update index.md * Update url-rewriting.md * Add files via upload * Add figures for redirect and rewrite * Update images * Update url-rewriting.md * Update RewriteRule.cs * Update Startup.cs * Minor image update * Add additional resource * Alphabetize project.json sections * Group System usings * Create README.md * Move sample info into the README * Delete sample info (info moved to README) * Add console logging dep * Add console logging * Alphabetize * Create License.txt * Update index.md * Create url_redirect.png * Add files via upload * Create project.json * Add files via upload * Create README.md * Create url-rewriting.md * Update toc.md * Delete add_apache_mod_redirect.png * Delete add_iis_url_rewrite.png * Delete add_redirect.png * Delete add_redirect_jpg_requests.png * Delete add_redirect_png_requests.png * Delete add_redirect_to_https.png * Delete add_redirect_to_https_permanent.png * Delete add_redirect_xml_requests.png * Delete add_rewrite.png * Delete url_redirect.png * Delete url_rewrite.png * Delete ApacheModRewrite.txt * Delete IISUrlRewrite.xml * Delete License.txt * Delete README.md * Delete RewriteRule.cs * Delete Startup.cs * Delete global.json * Delete project.json * Delete testCert.pfx * Delete url-rewriting.md * Update toc.md * Add link to middleware doc * Update url-rewriting.md * Update RewriteRule.cs * Update Startup.cs * Swap redirect+rewrite images * React to review feedback * Minor update * Fix PathString * Fix PathString * Add unsupported IIS feature issue links * Add trackAllCaptures PR link * Update sample * Update sample * Update images * Fix missing fundamentals node on doc link * React to feedback * Revert images * Remove heading code style * React to feedback * Update title and middleware heading levels * Update section heading and remove paragraph
2017-01-11 06:25:00 +08:00
}