This repository has been archived by the owner on Dec 14, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2.1k
/
RedirectResultExecutor.cs
73 lines (61 loc) · 2.44 KB
/
RedirectResultExecutor.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
// 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.Internal;
using Microsoft.AspNetCore.Mvc.Routing;
using Microsoft.Extensions.Logging;
using Microsoft.Net.Http.Headers;
namespace Microsoft.AspNetCore.Mvc.Infrastructure
{
public class RedirectResultExecutor : IActionResultExecutor<RedirectResult>
{
private readonly ILogger _logger;
private readonly IUrlHelperFactory _urlHelperFactory;
public RedirectResultExecutor(ILoggerFactory loggerFactory, IUrlHelperFactory urlHelperFactory)
{
if (loggerFactory == null)
{
throw new ArgumentNullException(nameof(loggerFactory));
}
if (urlHelperFactory == null)
{
throw new ArgumentNullException(nameof(urlHelperFactory));
}
_logger = loggerFactory.CreateLogger<RedirectResultExecutor>();
_urlHelperFactory = urlHelperFactory;
}
/// <inheritdoc />
public virtual Task ExecuteAsync(ActionContext context, RedirectResult result)
{
if (context == null)
{
throw new ArgumentNullException(nameof(context));
}
if (result == null)
{
throw new ArgumentNullException(nameof(result));
}
var urlHelper = result.UrlHelper ?? _urlHelperFactory.GetUrlHelper(context);
// IsLocalUrl is called to handle URLs starting with '~/'.
var destinationUrl = result.Url;
if (urlHelper.IsLocalUrl(destinationUrl))
{
destinationUrl = urlHelper.Content(result.Url);
}
_logger.RedirectResultExecuting(destinationUrl);
if (result.PreserveMethod)
{
context.HttpContext.Response.StatusCode = result.Permanent ?
StatusCodes.Status308PermanentRedirect : StatusCodes.Status307TemporaryRedirect;
context.HttpContext.Response.Headers[HeaderNames.Location] = destinationUrl;
}
else
{
context.HttpContext.Response.Redirect(destinationUrl, result.Permanent);
}
return Task.CompletedTask;
}
}
}