Set path in DefaultPageFactory (#5911)

This commit is contained in:
Ryan Brandenburg 2017-03-09 16:15:43 -08:00 committed by GitHub
parent ee4457f012
commit 7f3f6957be
4 changed files with 47 additions and 1 deletions

View File

@ -61,6 +61,7 @@ namespace Microsoft.AspNetCore.Mvc.RazorPages.Infrastructure
{
var page = (Page)activatorFactory(context);
page.PageContext = context;
page.Path = context.ActionDescriptor.RelativePath;
propertyActivator.Activate(page, context);
return page;
};

View File

@ -18,6 +18,20 @@ namespace Microsoft.AspNetCore.Mvc.FunctionalTests
public HttpClient Client { get; }
[Fact]
public async Task Page_SetsPath()
{
// Arrange
var request = new HttpRequestMessage(HttpMethod.Get, "http://localhost/PathSet");
// Act
var response = await Client.SendAsync(request);
// Assert
var content = await response.Content.ReadAsStringAsync();
Assert.Equal("Path: /PathSet.cshtml", content.Trim());
}
[Fact]
public async Task NoPage_NotFound()
{

View File

@ -49,7 +49,10 @@ namespace Microsoft.AspNetCore.Mvc.RazorPages.Infrastructure
{
PageTypeInfo = typeof(TestPage).GetTypeInfo(),
};
var pageContext = new PageContext();
var pageContext = new PageContext
{
ActionDescriptor = descriptor
};
var factoryProvider = CreatePageFactory();
// Act
@ -94,6 +97,31 @@ namespace Microsoft.AspNetCore.Mvc.RazorPages.Infrastructure
Assert.NotNull(testPage.ViewData);
}
[Fact]
public void PageFactorySetsPath()
{
// Arrange
var descriptor = new CompiledPageActionDescriptor
{
PageTypeInfo = typeof(ViewDataTestPage).GetTypeInfo(),
ModelTypeInfo = typeof(ViewDataTestPageModel).GetTypeInfo()
};
descriptor.RelativePath = "/this/is/a/path.cshtml";
var pageContext = new PageContext
{
ActionDescriptor = descriptor
};
// Act
var factory = CreatePageFactory().CreatePageFactory(descriptor);
var instance = factory(pageContext);
// Assert
var testPage = Assert.IsType<ViewDataTestPage>(instance);
Assert.Equal("/this/is/a/path.cshtml", testPage.Path);
}
[Fact]
public void PageFactorySetViewDataWithModelTypeWhenNotNull()
{

View File

@ -0,0 +1,3 @@
@page
Path: @Path