Use status code constants

This commit is contained in:
Mikael Mengistu 2016-10-06 16:23:55 -07:00 committed by GitHub
parent cd833a7d63
commit 9814f3bfbc
5 changed files with 15 additions and 12 deletions

View File

@ -1,13 +1,15 @@
// Copyright (c) .NET Foundation. All rights reserved. // 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. // Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
using Microsoft.AspNetCore.Http;
namespace Microsoft.AspNetCore.Rewrite.Internal.IISUrlRewrite namespace Microsoft.AspNetCore.Rewrite.Internal.IISUrlRewrite
{ {
public enum RedirectType public enum RedirectType
{ {
Permanent = 301, Permanent = StatusCodes.Status301MovedPermanently,
Found = 302, Found = StatusCodes.Status302Found,
SeeOther = 303, SeeOther = StatusCodes.Status303SeeOther,
Temporary = 307 Temporary = StatusCodes.Status307TemporaryRedirect
} }
} }

View File

@ -75,7 +75,7 @@ namespace Microsoft.AspNetCore.Rewrite.Internal.UrlActions
QueryString.FromUriComponent( QueryString.FromUriComponent(
pattern.Substring(split))); pattern.Substring(split)));
// not using the response.redirect here because status codes may be 301, 302, 307, 308 // not using the response.redirect here because status codes may be 301, 302, 307, 308
response.Headers[HeaderNames.Location] = pathBase + pattern.Substring(0, split) + query; response.Headers[HeaderNames.Location] = pathBase + pattern.Substring(0, split) + query;
} }
else else

View File

@ -2,6 +2,7 @@
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. // Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
using System; using System;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Rewrite.Internal; using Microsoft.AspNetCore.Rewrite.Internal;
namespace Microsoft.AspNetCore.Rewrite namespace Microsoft.AspNetCore.Rewrite
@ -58,7 +59,7 @@ namespace Microsoft.AspNetCore.Rewrite
/// <returns>The Rewrite options.</returns> /// <returns>The Rewrite options.</returns>
public static RewriteOptions AddRedirect(this RewriteOptions options, string regex, string replacement) public static RewriteOptions AddRedirect(this RewriteOptions options, string regex, string replacement)
{ {
return AddRedirect(options, regex, replacement, statusCode: 302); return AddRedirect(options, regex, replacement, statusCode: StatusCodes.Status302Found);
} }
/// <summary> /// <summary>
@ -83,7 +84,7 @@ namespace Microsoft.AspNetCore.Rewrite
/// <returns></returns> /// <returns></returns>
public static RewriteOptions AddRedirectToHttpsPermanent(this RewriteOptions options) public static RewriteOptions AddRedirectToHttpsPermanent(this RewriteOptions options)
{ {
return AddRedirectToHttps(options, statusCode: 301, sslPort: null); return AddRedirectToHttps(options, statusCode: StatusCodes.Status301MovedPermanently, sslPort: null);
} }
/// <summary> /// <summary>
@ -92,7 +93,7 @@ namespace Microsoft.AspNetCore.Rewrite
/// <param name="options">The <see cref="RewriteOptions"/>.</param> /// <param name="options">The <see cref="RewriteOptions"/>.</param>
public static RewriteOptions AddRedirectToHttps(this RewriteOptions options) public static RewriteOptions AddRedirectToHttps(this RewriteOptions options)
{ {
return AddRedirectToHttps(options, statusCode: 302, sslPort: null); return AddRedirectToHttps(options, statusCode: StatusCodes.Status302Found, sslPort: null);
} }
/// <summary> /// <summary>

View File

@ -247,7 +247,7 @@ namespace Microsoft.AspNetCore.Rewrite.Tests.ModRewrite
var response = await server.CreateClient().GetAsync(input); var response = await server.CreateClient().GetAsync(input);
Assert.Equal(response.StatusCode, (HttpStatusCode)301); Assert.Equal(response.StatusCode, (HttpStatusCode)301);
Assert.Equal(response.Headers.Location.AbsoluteUri, @"https://www.example.com/foo/"); Assert.Equal(@"https://www.example.com/foo/", response.Headers.Location.AbsoluteUri);
} }
[Theory] [Theory]

View File

@ -38,7 +38,7 @@ namespace Microsoft.AspNetCore.Rewrite.Tests.CodeRules
[Fact] [Fact]
public async Task CheckRedirectPath() public async Task CheckRedirectPath()
{ {
var options = new RewriteOptions().AddRedirect("(.*)","http://example.com/$1", statusCode: 301); var options = new RewriteOptions().AddRedirect("(.*)","http://example.com/$1", statusCode: StatusCodes.Status301MovedPermanently);
var builder = new WebHostBuilder() var builder = new WebHostBuilder()
.Configure(app => .Configure(app =>
{ {
@ -54,7 +54,7 @@ namespace Microsoft.AspNetCore.Rewrite.Tests.CodeRules
[Fact] [Fact]
public async Task CheckRedirectToHttps() public async Task CheckRedirectToHttps()
{ {
var options = new RewriteOptions().AddRedirectToHttps(statusCode: 301); var options = new RewriteOptions().AddRedirectToHttps(statusCode: StatusCodes.Status301MovedPermanently);
var builder = new WebHostBuilder() var builder = new WebHostBuilder()
.Configure(app => .Configure(app =>
{ {
@ -71,7 +71,7 @@ namespace Microsoft.AspNetCore.Rewrite.Tests.CodeRules
[Fact] [Fact]
public async Task CheckIfEmptyStringRedirectCorrectly() public async Task CheckIfEmptyStringRedirectCorrectly()
{ {
var options = new RewriteOptions().AddRedirect("(.*)", "$1", statusCode: 301); var options = new RewriteOptions().AddRedirect("(.*)", "$1", statusCode: StatusCodes.Status301MovedPermanently);
var builder = new WebHostBuilder() var builder = new WebHostBuilder()
.Configure(app => .Configure(app =>
{ {