Redirects with fragment (#5519)

- Added implementation of overloads where you can specify the fragment to redirect to.
- Added unit tests
- Added XML comments, including missing documentation of existing members
This commit is contained in:
Joonas Westlin 2016-12-06 20:32:01 +02:00 committed by Doug Bunting
parent 7178464ed2
commit 1dd1d49321
8 changed files with 429 additions and 7 deletions

View File

@ -469,7 +469,44 @@ namespace Microsoft.AspNetCore.Mvc
string controllerName, string controllerName,
object routeValues) object routeValues)
{ {
return new RedirectToActionResult(actionName, controllerName, routeValues) return RedirectToAction(actionName, controllerName, routeValues, fragment: null);
}
/// <summary>
/// Redirects to the specified action using the specified <paramref name="actionName"/>,
/// <paramref name="controllerName"/>, and <paramref name="fragment"/>.
/// </summary>
/// <param name="actionName">The name of the action.</param>
/// <param name="controllerName">The name of the controller.</param>
/// <param name="fragment">The fragment to add to the URL.</param>
/// <returns>The created <see cref="RedirectToActionResult"/> for the response.</returns>
[NonAction]
public virtual RedirectToActionResult RedirectToAction(
string actionName,
string controllerName,
string fragment)
{
return RedirectToAction(actionName, controllerName, routeValues: null, fragment: fragment);
}
/// <summary>
/// Redirects to the specified action using the specified <paramref name="actionName"/>,
/// <paramref name="controllerName"/>, <paramref name="routeValues"/>,
/// and <paramref name="fragment"/>.
/// </summary>
/// <param name="actionName">The name of the action.</param>
/// <param name="controllerName">The name of the controller.</param>
/// <param name="routeValues">The parameters for a route.</param>
/// <param name="fragment">The fragment to add to the URL.</param>
/// <returns>The created <see cref="RedirectToActionResult"/> for the response.</returns>
[NonAction]
public virtual RedirectToActionResult RedirectToAction(
string actionName,
string controllerName,
object routeValues,
string fragment)
{
return new RedirectToActionResult(actionName, controllerName, routeValues, fragment)
{ {
UrlHelper = Url, UrlHelper = Url,
}; };
@ -513,6 +550,24 @@ namespace Microsoft.AspNetCore.Mvc
return RedirectToActionPermanent(actionName, controllerName, routeValues: null); return RedirectToActionPermanent(actionName, controllerName, routeValues: null);
} }
/// <summary>
/// Redirects to the specified action with <see cref="RedirectToActionResult.Permanent"/> set to true
/// using the specified <paramref name="actionName"/>,
/// <paramref name="controllerName"/>, and <paramref name="fragment"/>.
/// </summary>
/// <param name="actionName">The name of the action.</param>
/// <param name="controllerName">The name of the controller.</param>
/// <param name="fragment">The fragment to add to the URL.</param>
/// <returns>The created <see cref="RedirectToActionResult"/> for the response.</returns>
[NonAction]
public virtual RedirectToActionResult RedirectToActionPermanent(
string actionName,
string controllerName,
string fragment)
{
return RedirectToActionPermanent(actionName, controllerName, routeValues: null, fragment: fragment);
}
/// <summary> /// <summary>
/// Redirects to the specified action with <see cref="RedirectToActionResult.Permanent"/> set to true /// Redirects to the specified action with <see cref="RedirectToActionResult.Permanent"/> set to true
/// using the specified <paramref name="actionName"/>, <paramref name="controllerName"/>, /// using the specified <paramref name="actionName"/>, <paramref name="controllerName"/>,
@ -527,12 +582,33 @@ namespace Microsoft.AspNetCore.Mvc
string actionName, string actionName,
string controllerName, string controllerName,
object routeValues) object routeValues)
{
return RedirectToActionPermanent(actionName, controllerName, routeValues, fragment: null);
}
/// <summary>
/// Redirects to the specified action with <see cref="RedirectToActionResult.Permanent"/> set to true
/// using the specified <paramref name="actionName"/>, <paramref name="controllerName"/>,
/// <paramref name="routeValues"/>, and <paramref name="fragment"/>.
/// </summary>
/// <param name="actionName">The name of the action.</param>
/// <param name="controllerName">The name of the controller.</param>
/// <param name="routeValues">The parameters for a route.</param>
/// <param name="fragment">The fragment to add to the URL.</param>
/// <returns>The created <see cref="RedirectToActionResult"/> for the response.</returns>
[NonAction]
public virtual RedirectToActionResult RedirectToActionPermanent(
string actionName,
string controllerName,
object routeValues,
string fragment)
{ {
return new RedirectToActionResult( return new RedirectToActionResult(
actionName, actionName,
controllerName, controllerName,
routeValues, routeValues,
permanent: true) permanent: true,
fragment: fragment)
{ {
UrlHelper = Url, UrlHelper = Url,
}; };
@ -570,7 +646,37 @@ namespace Microsoft.AspNetCore.Mvc
[NonAction] [NonAction]
public virtual RedirectToRouteResult RedirectToRoute(string routeName, object routeValues) public virtual RedirectToRouteResult RedirectToRoute(string routeName, object routeValues)
{ {
return new RedirectToRouteResult(routeName, routeValues) return RedirectToRoute(routeName, routeValues, fragment: null);
}
/// <summary>
/// Redirects to the specified route using the specified <paramref name="routeName"/>
/// and <paramref name="fragment"/>.
/// </summary>
/// <param name="routeName">The name of the route.</param>
/// <param name="fragment">The fragment to add to the URL.</param>
/// <returns>The created <see cref="RedirectToRouteResult"/> for the response.</returns>
[NonAction]
public virtual RedirectToRouteResult RedirectToRoute(string routeName, string fragment)
{
return RedirectToRoute(routeName, routeValues: null, fragment: fragment);
}
/// <summary>
/// Redirects to the specified route using the specified <paramref name="routeName"/>,
/// <paramref name="routeValues"/>, and <paramref name="fragment"/>.
/// </summary>
/// <param name="routeName">The name of the route.</param>
/// <param name="routeValues">The parameters for a route.</param>
/// <param name="fragment">The fragment to add to the URL.</param>
/// <returns>The created <see cref="RedirectToRouteResult"/> for the response.</returns>
[NonAction]
public virtual RedirectToRouteResult RedirectToRoute(
string routeName,
object routeValues,
string fragment)
{
return new RedirectToRouteResult(routeName, routeValues, fragment)
{ {
UrlHelper = Url, UrlHelper = Url,
}; };
@ -610,7 +716,38 @@ namespace Microsoft.AspNetCore.Mvc
[NonAction] [NonAction]
public virtual RedirectToRouteResult RedirectToRoutePermanent(string routeName, object routeValues) public virtual RedirectToRouteResult RedirectToRoutePermanent(string routeName, object routeValues)
{ {
return new RedirectToRouteResult(routeName, routeValues, permanent: true) return RedirectToRoutePermanent(routeName, routeValues, fragment: null);
}
/// <summary>
/// Redirects to the specified route with <see cref="RedirectToRouteResult.Permanent"/> set to true
/// using the specified <paramref name="routeName"/> and <paramref name="fragment"/>.
/// </summary>
/// <param name="routeName">The name of the route.</param>
/// <param name="fragment">The fragment to add to the URL.</param>
/// <returns>The created <see cref="RedirectToRouteResult"/> for the response.</returns>
[NonAction]
public virtual RedirectToRouteResult RedirectToRoutePermanent(string routeName, string fragment)
{
return RedirectToRoutePermanent(routeName, routeValues: null, fragment: fragment);
}
/// <summary>
/// Redirects to the specified route with <see cref="RedirectToRouteResult.Permanent"/> set to true
/// using the specified <paramref name="routeName"/>, <paramref name="routeValues"/>,
/// and <paramref name="fragment"/>.
/// </summary>
/// <param name="routeName">The name of the route.</param>
/// <param name="routeValues">The parameters for a route.</param>
/// <param name="fragment">The fragment to add to the URL.</param>
/// <returns>The created <see cref="RedirectToRouteResult"/> for the response.</returns>
[NonAction]
public virtual RedirectToRouteResult RedirectToRoutePermanent(
string routeName,
object routeValues,
string fragment)
{
return new RedirectToRouteResult(routeName, routeValues, permanent: true, fragment: fragment)
{ {
UrlHelper = Url, UrlHelper = Url,
}; };
@ -1082,7 +1219,7 @@ namespace Microsoft.AspNetCore.Mvc
/// <summary> /// <summary>
/// Creates a <see cref="AcceptedAtRouteResult"/> object that produces an Accepted (202) response. /// Creates a <see cref="AcceptedAtRouteResult"/> object that produces an Accepted (202) response.
/// </summary> /// </summary>
/// <param name="routeName">The name of the route to use for generating the URL.</param> /// <param name="routeName">The name of the route to use for generating the URL.</param>
/// <returns>The created <see cref="AcceptedAtRouteResult"/> for the response.</returns> /// <returns>The created <see cref="AcceptedAtRouteResult"/> for the response.</returns>
[NonAction] [NonAction]
public virtual AcceptedAtRouteResult AcceptedAtRoute(string routeName) public virtual AcceptedAtRouteResult AcceptedAtRoute(string routeName)

View File

@ -33,7 +33,13 @@ namespace Microsoft.AspNetCore.Mvc.Internal
{ {
var urlHelper = result.UrlHelper ?? _urlHelperFactory.GetUrlHelper(context); var urlHelper = result.UrlHelper ?? _urlHelperFactory.GetUrlHelper(context);
var destinationUrl = urlHelper.Action(result.ActionName, result.ControllerName, result.RouteValues); var destinationUrl = urlHelper.Action(
result.ActionName,
result.ControllerName,
result.RouteValues,
protocol: null,
host: null,
fragment: result.Fragment);
if (string.IsNullOrEmpty(destinationUrl)) if (string.IsNullOrEmpty(destinationUrl))
{ {
throw new InvalidOperationException(Resources.NoRoutesMatched); throw new InvalidOperationException(Resources.NoRoutesMatched);

View File

@ -33,7 +33,12 @@ namespace Microsoft.AspNetCore.Mvc.Internal
{ {
var urlHelper = result.UrlHelper ?? _urlHelperFactory.GetUrlHelper(context); var urlHelper = result.UrlHelper ?? _urlHelperFactory.GetUrlHelper(context);
var destinationUrl = urlHelper.RouteUrl(result.RouteName, result.RouteValues); var destinationUrl = urlHelper.RouteUrl(
result.RouteName,
result.RouteValues,
protocol: null,
host: null,
fragment: result.Fragment);
if (string.IsNullOrEmpty(destinationUrl)) if (string.IsNullOrEmpty(destinationUrl))
{ {
throw new InvalidOperationException(Resources.NoRoutesMatched); throw new InvalidOperationException(Resources.NoRoutesMatched);

View File

@ -9,8 +9,20 @@ using Microsoft.Extensions.DependencyInjection;
namespace Microsoft.AspNetCore.Mvc namespace Microsoft.AspNetCore.Mvc
{ {
/// <summary>
/// An <see cref="ActionResult"/> that returns a Found (302)
/// or Moved Permanently (301) response with a Location header.
/// Targets a controller action.
/// </summary>
public class RedirectToActionResult : ActionResult, IKeepTempDataResult public class RedirectToActionResult : ActionResult, IKeepTempDataResult
{ {
/// <summary>
/// Initializes a new instance of the <see cref="RedirectToActionResult"/> with the values
/// provided.
/// </summary>
/// <param name="actionName">The name of the action to use for generating the URL.</param>
/// <param name="controllerName">The name of the controller to use for generating the URL.</param>
/// <param name="routeValues">The route data to use for generating the URL.</param>
public RedirectToActionResult( public RedirectToActionResult(
string actionName, string actionName,
string controllerName, string controllerName,
@ -19,16 +31,61 @@ namespace Microsoft.AspNetCore.Mvc
{ {
} }
/// <summary>
/// Initializes a new instance of the <see cref="RedirectToActionResult"/> with the values
/// provided.
/// </summary>
/// <param name="actionName">The name of the action to use for generating the URL.</param>
/// <param name="controllerName">The name of the controller to use for generating the URL.</param>
/// <param name="routeValues">The route data to use for generating the URL.</param>
/// <param name="fragment">The fragment to add to the URL.</param>
public RedirectToActionResult(
string actionName,
string controllerName,
object routeValues,
string fragment)
: this(actionName, controllerName, routeValues, permanent: false, fragment: fragment)
{
}
/// <summary>
/// Initializes a new instance of the <see cref="RedirectToActionResult"/> with the values
/// provided.
/// </summary>
/// <param name="actionName">The name of the action to use for generating the URL.</param>
/// <param name="controllerName">The name of the controller to use for generating the URL.</param>
/// <param name="routeValues">The route data to use for generating the URL.</param>
/// <param name="permanent">If set to true, makes the redirect permanent (301). Otherwise a temporary redirect is used (302).</param>
public RedirectToActionResult( public RedirectToActionResult(
string actionName, string actionName,
string controllerName, string controllerName,
object routeValues, object routeValues,
bool permanent) bool permanent)
: this(actionName, controllerName, routeValues, permanent, fragment: null)
{
}
/// <summary>
/// Initializes a new instance of the <see cref="RedirectToActionResult"/> with the values
/// provided.
/// </summary>
/// <param name="actionName">The name of the action to use for generating the URL.</param>
/// <param name="controllerName">The name of the controller to use for generating the URL.</param>
/// <param name="routeValues">The route data to use for generating the URL.</param>
/// <param name="permanent">If set to true, makes the redirect permanent (301). Otherwise a temporary redirect is used (302).</param>
/// <param name="fragment">The fragment to add to the URL.</param>
public RedirectToActionResult(
string actionName,
string controllerName,
object routeValues,
bool permanent,
string fragment)
{ {
ActionName = actionName; ActionName = actionName;
ControllerName = controllerName; ControllerName = controllerName;
RouteValues = routeValues == null ? null : new RouteValueDictionary(routeValues); RouteValues = routeValues == null ? null : new RouteValueDictionary(routeValues);
Permanent = permanent; Permanent = permanent;
Fragment = fragment;
} }
/// <summary> /// <summary>
@ -51,8 +108,16 @@ namespace Microsoft.AspNetCore.Mvc
/// </summary> /// </summary>
public RouteValueDictionary RouteValues { get; set; } public RouteValueDictionary RouteValues { get; set; }
/// <summary>
/// Gets or sets an indication that the redirect is permanent.
/// </summary>
public bool Permanent { get; set; } public bool Permanent { get; set; }
/// <summary>
/// Gets or sets the fragment to add to the URL.
/// </summary>
public string Fragment { get; set; }
/// <inheritdoc /> /// <inheritdoc />
public override void ExecuteResult(ActionContext context) public override void ExecuteResult(ActionContext context)
{ {

View File

@ -9,13 +9,29 @@ using Microsoft.Extensions.DependencyInjection;
namespace Microsoft.AspNetCore.Mvc namespace Microsoft.AspNetCore.Mvc
{ {
/// <summary>
/// An <see cref="ActionResult"/> that returns a Found (302)
/// or Moved Permanently (301) response with a Location header.
/// Targets a registered route.
/// </summary>
public class RedirectToRouteResult : ActionResult, IKeepTempDataResult public class RedirectToRouteResult : ActionResult, IKeepTempDataResult
{ {
/// <summary>
/// Initializes a new instance of the <see cref="RedirectToRouteResult"/> with the values
/// provided.
/// </summary>
/// <param name="routeValues">The parameters for the route.</param>
public RedirectToRouteResult(object routeValues) public RedirectToRouteResult(object routeValues)
: this(routeName: null, routeValues: routeValues) : this(routeName: null, routeValues: routeValues)
{ {
} }
/// <summary>
/// Initializes a new instance of the <see cref="RedirectToRouteResult"/> with the values
/// provided.
/// </summary>
/// <param name="routeName">The name of the route.</param>
/// <param name="routeValues">The parameters for the route.</param>
public RedirectToRouteResult( public RedirectToRouteResult(
string routeName, string routeName,
object routeValues) object routeValues)
@ -23,14 +39,54 @@ namespace Microsoft.AspNetCore.Mvc
{ {
} }
/// <summary>
/// Initializes a new instance of the <see cref="RedirectToRouteResult"/> with the values
/// provided.
/// </summary>
/// <param name="routeName">The name of the route.</param>
/// <param name="routeValues">The parameters for the route.</param>
/// <param name="permanent">If set to true, makes the redirect permanent (301). Otherwise a temporary redirect is used (302).</param>
public RedirectToRouteResult( public RedirectToRouteResult(
string routeName, string routeName,
object routeValues, object routeValues,
bool permanent) bool permanent)
: this(routeName, routeValues, permanent, fragment: null)
{
}
/// <summary>
/// Initializes a new instance of the <see cref="RedirectToRouteResult"/> with the values
/// provided.
/// </summary>
/// <param name="routeName">The name of the route.</param>
/// <param name="routeValues">The parameters for the route.</param>
/// <param name="fragment">The fragment to add to the URL.</param>
public RedirectToRouteResult(
string routeName,
object routeValues,
string fragment)
: this(routeName, routeValues, permanent: false, fragment: fragment)
{
}
/// <summary>
/// Initializes a new instance of the <see cref="RedirectToRouteResult"/> with the values
/// provided.
/// </summary>
/// <param name="routeName">The name of the route.</param>
/// <param name="routeValues">The parameters for the route.</param>
/// <param name="permanent">If set to true, makes the redirect permanent (301). Otherwise a temporary redirect is used (302).</param>
/// <param name="fragment">The fragment to add to the URL.</param>
public RedirectToRouteResult(
string routeName,
object routeValues,
bool permanent,
string fragment)
{ {
RouteName = routeName; RouteName = routeName;
RouteValues = routeValues == null ? null : new RouteValueDictionary(routeValues); RouteValues = routeValues == null ? null : new RouteValueDictionary(routeValues);
Permanent = permanent; Permanent = permanent;
Fragment = fragment;
} }
/// <summary> /// <summary>
@ -48,8 +104,16 @@ namespace Microsoft.AspNetCore.Mvc
/// </summary> /// </summary>
public RouteValueDictionary RouteValues { get; set; } public RouteValueDictionary RouteValues { get; set; }
/// <summary>
/// Gets or sets an indication that the redirect is permanent.
/// </summary>
public bool Permanent { get; set; } public bool Permanent { get; set; }
/// <summary>
/// Gets or sets the fragment to add to the URL.
/// </summary>
public string Fragment { get; set; }
/// <inheritdoc /> /// <inheritdoc />
public override void ExecuteResult(ActionContext context) public override void ExecuteResult(ActionContext context)
{ {

View File

@ -274,6 +274,30 @@ namespace Microsoft.AspNetCore.Mvc.Core.Test
Assert.Equal(expected, resultTemporary.RouteValues); Assert.Equal(expected, resultTemporary.RouteValues);
} }
[Theory]
[MemberData(nameof(RedirectTestData))]
public void RedirectToAction_WithParameterActionAndControllerAndRouteValuesAndFragment_SetsResultProperties(
object routeValues,
IEnumerable<KeyValuePair<string, object>> expectedRouteValues)
{
// Arrange
var controller = new TestableController();
var expectedAction = "Action";
var expectedController = "Home";
var expectedFragment = "test";
// Act
var result = controller.RedirectToAction("Action", "Home", routeValues, "test");
// Assert
Assert.IsType<RedirectToActionResult>(result);
Assert.False(result.Permanent);
Assert.Equal(expectedAction, result.ActionName);
Assert.Equal(expectedRouteValues, result.RouteValues);
Assert.Equal(expectedController, result.ControllerName);
Assert.Equal(expectedFragment, result.Fragment);
}
[Theory] [Theory]
[MemberData(nameof(RedirectTestData))] [MemberData(nameof(RedirectTestData))]
public void RedirectToActionPermanent_WithParameterActionAndRouteValues_SetsResultProperties( public void RedirectToActionPermanent_WithParameterActionAndRouteValues_SetsResultProperties(
@ -293,6 +317,30 @@ namespace Microsoft.AspNetCore.Mvc.Core.Test
Assert.Equal(expected, resultPermanent.RouteValues); Assert.Equal(expected, resultPermanent.RouteValues);
} }
[Theory]
[MemberData(nameof(RedirectTestData))]
public void RedirectToActionPermanent_WithParameterActionAndControllerAndRouteValuesAndFragment_SetsResultProperties(
object routeValues,
IEnumerable<KeyValuePair<string, object>> expectedRouteValues)
{
// Arrange
var controller = new TestableController();
var expectedAction = "Action";
var expectedController = "Home";
var expectedFragment = "test";
// Act
var result = controller.RedirectToActionPermanent("Action", "Home", routeValues, "test");
// Assert
Assert.IsType<RedirectToActionResult>(result);
Assert.True(result.Permanent);
Assert.Equal(expectedAction, result.ActionName);
Assert.Equal(expectedRouteValues, result.RouteValues);
Assert.Equal(expectedController, result.ControllerName);
Assert.Equal(expectedFragment, result.Fragment);
}
[Theory] [Theory]
[MemberData(nameof(RedirectTestData))] [MemberData(nameof(RedirectTestData))]
public void RedirectToRoute_WithParameterRouteValues_SetsResultEqualRouteValues( public void RedirectToRoute_WithParameterRouteValues_SetsResultEqualRouteValues(
@ -311,6 +359,28 @@ namespace Microsoft.AspNetCore.Mvc.Core.Test
Assert.Equal(expected, resultTemporary.RouteValues); Assert.Equal(expected, resultTemporary.RouteValues);
} }
[Theory]
[MemberData(nameof(RedirectTestData))]
public void RedirectToRoute_WithParameterRouteNameAndRouteValuesAndFragment_SetsResultProperties(
object routeValues,
IEnumerable<KeyValuePair<string, object>> expectedRouteValues)
{
// Arrange
var controller = new TestableController();
var expectedRoute = "TestRoute";
var expectedFragment = "test";
// Act
var result = controller.RedirectToRoute("TestRoute", routeValues, "test");
// Assert
Assert.IsType<RedirectToRouteResult>(result);
Assert.False(result.Permanent);
Assert.Equal(expectedRoute, result.RouteName);
Assert.Equal(expectedRouteValues, result.RouteValues);
Assert.Equal(expectedFragment, result.Fragment);
}
[Theory] [Theory]
[MemberData(nameof(RedirectTestData))] [MemberData(nameof(RedirectTestData))]
public void RedirectToRoutePermanent_WithParameterRouteValues_SetsResultEqualRouteValuesAndPermanent( public void RedirectToRoutePermanent_WithParameterRouteValues_SetsResultEqualRouteValuesAndPermanent(
@ -329,6 +399,28 @@ namespace Microsoft.AspNetCore.Mvc.Core.Test
Assert.Equal(expected, resultPermanent.RouteValues); Assert.Equal(expected, resultPermanent.RouteValues);
} }
[Theory]
[MemberData(nameof(RedirectTestData))]
public void RedirectToRoutePermanent_WithParameterRouteNameAndRouteValuesAndFragment_SetsResultProperties(
object routeValues,
IEnumerable<KeyValuePair<string, object>> expectedRouteValues)
{
// Arrange
var controller = new TestableController();
var expectedRoute = "TestRoute";
var expectedFragment = "test";
// Act
var result = controller.RedirectToRoutePermanent("TestRoute", routeValues, "test");
// Assert
Assert.IsType<RedirectToRouteResult>(result);
Assert.True(result.Permanent);
Assert.Equal(expectedRoute, result.RouteName);
Assert.Equal(expectedRouteValues, result.RouteValues);
Assert.Equal(expectedFragment, result.Fragment);
}
[Fact] [Fact]
public void RedirectToRoute_WithParameterRouteName_SetsResultSameRouteName() public void RedirectToRoute_WithParameterRouteName_SetsResultSameRouteName()
{ {

View File

@ -83,6 +83,34 @@ namespace Microsoft.AspNetCore.Mvc
"No route matches the supplied values."); "No route matches the supplied values.");
} }
[Fact]
public async Task RedirectToAction_Execute_WithFragment_PassesCorrectValuesToRedirect()
{
// Arrange
var expectedUrl = "/Home/SampleAction#test";
var expectedStatusCode = StatusCodes.Status302Found;
var httpContext = new DefaultHttpContext
{
RequestServices = CreateServices().BuildServiceProvider(),
};
var actionContext = new ActionContext(httpContext, new RouteData(), new ActionDescriptor());
var urlHelper = GetMockUrlHelper(expectedUrl);
var result = new RedirectToActionResult("SampleAction", "Home", null, false, "test")
{
UrlHelper = urlHelper,
};
// Act
await result.ExecuteResultAsync(actionContext);
// Assert
Assert.Equal(expectedStatusCode, httpContext.Response.StatusCode);
Assert.Equal(expectedUrl, httpContext.Response.Headers["Location"]);
}
private static IUrlHelper GetMockUrlHelper(string returnValue) private static IUrlHelper GetMockUrlHelper(string returnValue)
{ {
var urlHelper = new Mock<IUrlHelper>(); var urlHelper = new Mock<IUrlHelper>();

View File

@ -116,6 +116,31 @@ namespace Microsoft.AspNetCore.Mvc
Assert.Equal(locationUrl, httpContext.Response.Headers["Location"]); Assert.Equal(locationUrl, httpContext.Response.Headers["Location"]);
} }
[Fact]
public async Task ExecuteResultAsync_WithFragment_PassesCorrectValuesToRedirect()
{
// Arrange
var expectedUrl = "/SampleAction#test";
var expectedStatusCode = StatusCodes.Status301MovedPermanently;
var httpContext = GetHttpContext();
var actionContext = new ActionContext(httpContext, new RouteData(), new ActionDescriptor());
var urlHelper = GetMockUrlHelper(expectedUrl);
var result = new RedirectToRouteResult("Sample", null, true, "test")
{
UrlHelper = urlHelper,
};
// Act
await result.ExecuteResultAsync(actionContext);
// Assert
Assert.Equal(expectedStatusCode, httpContext.Response.StatusCode);
Assert.Equal(expectedUrl, httpContext.Response.Headers["Location"]);
}
private static HttpContext GetHttpContext(IUrlHelperFactory factory = null) private static HttpContext GetHttpContext(IUrlHelperFactory factory = null)
{ {
var services = CreateServices(factory); var services = CreateServices(factory);