RedirectToPage doesn't trim /Index from URL

Fixes #6080
This commit is contained in:
Pranav K 2017-04-19 19:16:39 -07:00
parent a975d7fa01
commit 681b798a2e
4 changed files with 44 additions and 2 deletions

View File

@ -88,6 +88,8 @@ namespace Microsoft.AspNetCore.Mvc.RazorPages.Infrastructure
Name = selector.AttributeRouteModel.Name,
Order = selector.AttributeRouteModel.Order ?? 0,
Template = selector.AttributeRouteModel.Template,
SuppressLinkGeneration = selector.AttributeRouteModel.SuppressLinkGeneration,
SuppressPathMatching = selector.AttributeRouteModel.SuppressPathMatching,
},
DisplayName = $"Page: {model.ViewEnginePath}",
FilterDescriptors = filters,
@ -95,7 +97,7 @@ namespace Microsoft.AspNetCore.Mvc.RazorPages.Infrastructure
RelativePath = model.RelativePath,
RouteValues = new Dictionary<string, string>(StringComparer.OrdinalIgnoreCase)
{
{ "page", model.ViewEnginePath},
{ "page", model.ViewEnginePath },
},
ViewEnginePath = model.ViewEnginePath,
});

View File

@ -20,11 +20,16 @@ namespace Microsoft.AspNetCore.Mvc.RazorPages.Internal
model.RelativePath));
}
model.Selectors.Add(CreateSelectorModel(model.ViewEnginePath, routeTemplate));
var selectorModel = CreateSelectorModel(model.ViewEnginePath, routeTemplate);
model.Selectors.Add(selectorModel);
var fileName = Path.GetFileName(model.RelativePath);
if (string.Equals(IndexFileName, fileName, StringComparison.OrdinalIgnoreCase))
{
// For pages ending in /Index.cshtml, we want to allow incoming routing, but
// force outgoing routes to match to the path sans /Index.
selectorModel.AttributeRouteModel.SuppressLinkGeneration = true;
var parentDirectoryPath = model.ViewEnginePath;
var index = parentDirectoryPath.LastIndexOf('/');
if (index == -1)

View File

@ -142,5 +142,33 @@ namespace Microsoft.AspNetCore.Mvc.FunctionalTests
// Assert
Assert.Equal(expected, response.Trim());
}
[Fact]
public async Task RedirectFromPage_RedirectsToPathWithoutIndexSegment()
{
//Arrange
var expected = "/Redirects";
// Act
var response = await Client.GetAsync("/Redirects/Index");
// Assert
Assert.Equal(HttpStatusCode.Redirect, response.StatusCode);
Assert.Equal(expected, response.Headers.Location.ToString());
}
[Fact]
public async Task RedirectFromPage_ToIndex_RedirectsToPathWithoutIndexSegment()
{
//Arrange
var expected = "/Redirects";
// Act
var response = await Client.GetAsync("/Redirects/RedirectToIndex");
// Assert
Assert.Equal(HttpStatusCode.Redirect, response.StatusCode);
Assert.Equal(expected, response.Headers.Location.ToString());
}
}
}

View File

@ -0,0 +1,7 @@
@page "{formaction?}"
@functions
{
public IActionResult OnGet() => RedirectToPage();
public IActionResult OnGetRedirectToIndex() => RedirectToPage("/Redirects/Index");
}