Skip to content
This repository has been archived by the owner on Dec 14, 2018. It is now read-only.

Commit

Permalink
[Fixes #2545] RedirectToRouteResult doesn't use RouteName property wh…
Browse files Browse the repository at this point in the history
…en calculating destination URL
  • Loading branch information
kichalla committed May 12, 2015
1 parent b2318bc commit 6d29274
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ public override void ExecuteResult([NotNull] ActionContext context)
{
var urlHelper = GetUrlHelper(context);

var destinationUrl = urlHelper.RouteUrl(RouteValues);
var destinationUrl = urlHelper.RouteUrl(RouteName, RouteValues);
if (string.IsNullOrEmpty(destinationUrl))
{
throw new InvalidOperationException(Resources.NoRoutesMatched);
Expand Down
11 changes: 7 additions & 4 deletions test/Microsoft.AspNet.Mvc.FunctionalTests/LinkGenerationTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,12 @@ public class LinkGenerationTests
private readonly Assembly _resourcesAssembly = typeof(LinkGenerationTests).GetTypeInfo().Assembly;

[Theory]
[InlineData("http://pingüino/Home/RedirectToActionReturningTaskAction")]
[InlineData("http://pingüino/Home/RedirectToRouteActionAsMethodAction")]
public async Task GeneratedLinksWithActionResults_AreRelativeLinks_WhenSetOnLocationHeader(string url)
[InlineData("http://pingüino/Home/RedirectToActionReturningTaskAction", "/Home/ActionReturningTask")]
[InlineData("http://pingüino/Home/RedirectToRouteActionAsMethodAction", "/Home/ActionReturningTask")]
[InlineData("http://pingüino/Home/RedirectToRouteUsingRouteName", "/api/orders/10")]
public async Task GeneratedLinksWithActionResults_AreRelativeLinks_WhenSetOnLocationHeader(
string url,
string expected)
{
// Arrange
var server = TestHelper.CreateServer(_app, SiteName, _configureServices);
Expand All @@ -43,7 +46,7 @@ public async Task GeneratedLinksWithActionResults_AreRelativeLinks_WhenSetOnLoca
// Assert
Assert.Equal(HttpStatusCode.Redirect, response.StatusCode);

Assert.Equal("/Home/ActionReturningTask", response.Headers.Location.ToString());
Assert.Equal(expected, response.Headers.Location.ToString());
}

[Fact]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -327,19 +327,23 @@ public async Task ApiController_RedirectString()
Assert.Equal("http://localhost/api/Users", response.Headers.Location.OriginalString);
}

[Fact]
public async Task ApiController_RedirectUri()
[Theory]
[InlineData("http://localhost/api/Blog/ActionResult/GetRedirectUri", "api/Blog")]
[InlineData(
"http://localhost/api/Blog/ActionResult/GetRedirectUrlUsingRouteName",
"/api/Blog/BasicApi/WriteToHttpContext")]
public async Task ApiController_RedirectUri(string url, string expected)
{
// Arrange
var server = TestHelper.CreateServer(_app, SiteName, _configureServices);
var client = server.CreateClient();

// Act
var response = await client.GetAsync("http://localhost/api/Blog/ActionResult/GetRedirectUri");
var response = await client.GetAsync(url);

// Assert
Assert.Equal(HttpStatusCode.Redirect, response.StatusCode);
Assert.Equal("api/Blog", response.Headers.Location.OriginalString);
Assert.Equal(expected, response.Headers.Location.OriginalString);
}

[Fact]
Expand Down
7 changes: 6 additions & 1 deletion test/WebSites/BasicWebSite/Controllers/HomeController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,12 @@ public IActionResult RedirectToActionReturningTaskAction()

public IActionResult RedirectToRouteActionAsMethodAction()
{
return RedirectToRoute("ActionAsMethod", new { action = "ActionReturningTask", controller = "Home" });
return RedirectToRoute(new { action = "ActionReturningTask", controller = "Home" });
}

public IActionResult RedirectToRouteUsingRouteName()
{
return RedirectToRoute("OrdersApi", new { id = 10 });
}

public IActionResult NoContentResult()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,11 @@ public IActionResult GetRedirectUri()
return Redirect(new Uri("api/Blog", UriKind.RelativeOrAbsolute));
}

public IActionResult GetRedirectUrlUsingRouteName()
{
return RedirectToRoute("named-action", new { controller = "BasicApi", action = "WriteToHttpContext" });
}

public IActionResult GetResponseMessage()
{
var response = new HttpResponseMessage(HttpStatusCode.OK);
Expand Down

0 comments on commit 6d29274

Please sign in to comment.