This repository has been archived by the owner on Dec 14, 2018. It is now read-only.
/
RedirectResult.cs
156 lines (136 loc) · 5.65 KB
/
RedirectResult.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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
// 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.Threading.Tasks;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc.Core;
using Microsoft.AspNetCore.Mvc.Infrastructure;
using Microsoft.AspNetCore.Mvc.Internal;
using Microsoft.AspNetCore.Mvc.Routing;
using Microsoft.AspNetCore.Mvc.ViewFeatures;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
using Microsoft.Net.Http.Headers;
namespace Microsoft.AspNetCore.Mvc
{
/// <summary>
/// An <see cref="ActionResult"/> that returns a Found (302), Moved Permanently (301), Temporary Redirect (307),
/// or Permanent Redirect (308) response with a Location header to the supplied URL.
/// </summary>
public class RedirectResult : ActionResult, IKeepTempDataResult
{
private string _url;
/// <summary>
/// Initializes a new instance of the <see cref="RedirectResult"/> class with the values
/// provided.
/// </summary>
/// <param name="url">The local URL to redirect to.</param>
public RedirectResult(string url)
: this(url, permanent: false)
{
if (url == null)
{
throw new ArgumentNullException(nameof(url));
}
}
/// <summary>
/// Initializes a new instance of the <see cref="RedirectResult"/> class with the values
/// provided.
/// </summary>
/// <param name="url">The URL to redirect to.</param>
/// <param name="permanent">Specifies whether the redirect should be permanent (301) or temporary (302).</param>
public RedirectResult(string url, bool permanent)
: this(url, permanent, preserveMethod: false)
{
}
/// <summary>
/// Initializes a new instance of the <see cref="RedirectResult"/> class with the values
/// provided.
/// </summary>
/// <param name="url">The URL to redirect to.</param>
/// <param name="permanent">Specifies whether the redirect should be permanent (301) or temporary (302).</param>
/// <param name="preserveMethod">If set to true, make the temporary redirect (307) or permanent redirect (308) preserve the initial request method.</param>
public RedirectResult(string url, bool permanent, bool preserveMethod)
{
if (url == null)
{
throw new ArgumentNullException(nameof(url));
}
if (string.IsNullOrEmpty(url))
{
throw new ArgumentException(Resources.ArgumentCannotBeNullOrEmpty, nameof(url));
}
Permanent = permanent;
PreserveMethod = preserveMethod;
Url = url;
}
/// <summary>
/// Gets or sets the value that specifies that the redirect should be permanent if true or temporary if false.
/// </summary>
public bool Permanent { get; set; }
/// <summary>
/// Gets or sets an indication that the redirect preserves the initial request method.
/// </summary>
public bool PreserveMethod { get; set; }
/// <summary>
/// Gets or sets the URL to redirect to.
/// </summary>
public string Url
{
get => _url;
set
{
if (string.IsNullOrEmpty(value))
{
throw new ArgumentException(Resources.ArgumentCannotBeNullOrEmpty, nameof(value));
}
_url = value;
}
}
/// <summary>
/// Gets or sets the <see cref="IUrlHelper"/> for this result.
/// </summary>
public IUrlHelper UrlHelper { get; set; }
/// <inheritdoc />
public override Task ExecuteResultAsync(ActionContext context)
{
if (context == null)
{
throw new ArgumentNullException(nameof(context));
}
var executor = context.HttpContext.RequestServices.GetRequiredService<IActionResultExecutor<RedirectResult>>();
return executor.ExecuteAsync(context, this);
}
#pragma warning disable CS0809
[Obsolete("This implementation will be removed in a future release, use ExecuteResultAsync.")]
public override void ExecuteResult(ActionContext context)
{
if (context == null)
{
throw new ArgumentNullException(nameof(context));
}
var services = context.HttpContext.RequestServices;
var urlHelperFactory = services.GetRequiredService<IUrlHelperFactory>();
var logger = services.GetRequiredService<ILogger<RedirectResult>>();
var urlHelper = UrlHelper ?? urlHelperFactory.GetUrlHelper(context);
// IsLocalUrl is called to handle URLs starting with '~/'.
var destinationUrl = Url;
if (urlHelper.IsLocalUrl(destinationUrl))
{
destinationUrl = urlHelper.Content(Url);
}
logger.RedirectResultExecuting(destinationUrl);
if (PreserveMethod)
{
context.HttpContext.Response.StatusCode = Permanent ?
StatusCodes.Status308PermanentRedirect : StatusCodes.Status307TemporaryRedirect;
context.HttpContext.Response.Headers[HeaderNames.Location] = destinationUrl;
}
else
{
context.HttpContext.Response.Redirect(destinationUrl, Permanent);
}
}
#pragma warning restore CS0809
}
}