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

Commit

Permalink
_ViewStart.cshtml not picked up when added to the root of the app
Browse files Browse the repository at this point in the history
Fixes #6308
  • Loading branch information
pranavkm committed Jun 23, 2017
1 parent 0dfffd4 commit aa5a348
Show file tree
Hide file tree
Showing 5 changed files with 36 additions and 78 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,6 @@ public class PageActionInvokerProvider : IActionInvokerProvider
private readonly IModelMetadataProvider _modelMetadataProvider;
private readonly ITempDataDictionaryFactory _tempDataFactory;
private readonly HtmlHelperOptions _htmlHelperOptions;
private readonly RazorPagesOptions _razorPagesOptions;
private readonly IPageHandlerMethodSelector _selector;
private readonly RazorProject _razorProject;
private readonly DiagnosticSource _diagnosticSource;
Expand All @@ -56,7 +55,6 @@ public PageActionInvokerProvider(
ITempDataDictionaryFactory tempDataFactory,
IOptions<MvcOptions> mvcOptions,
IOptions<HtmlHelperOptions> htmlHelperOptions,
IOptions<RazorPagesOptions> razorPagesOptions,
IPageHandlerMethodSelector selector,
RazorProject razorProject,
DiagnosticSource diagnosticSource,
Expand All @@ -73,7 +71,6 @@ public PageActionInvokerProvider(
_modelMetadataProvider = modelMetadataProvider;
_tempDataFactory = tempDataFactory;
_htmlHelperOptions = htmlHelperOptions.Value;
_razorPagesOptions = razorPagesOptions.Value;
_selector = selector;
_razorProject = razorProject;
_diagnosticSource = diagnosticSource;
Expand Down Expand Up @@ -210,8 +207,8 @@ private PageActionInvokerCacheEntry CreateCacheEntry(
internal List<Func<IRazorPage>> GetViewStartFactories(CompiledPageActionDescriptor descriptor)
{
var viewStartFactories = new List<Func<IRazorPage>>();
// Always pick up all _ViewStarts, including the ones outside the Pages root.
var viewStartItems = _razorProject.FindHierarchicalItems(
_razorPagesOptions.RootDirectory,
descriptor.RelativePath,
ViewStartFileName);
foreach (var item in viewStartItems)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,20 @@ public async Task ViewStart_IsDiscoveredWhenRootDirectoryIsSpecified()
Assert.Equal(expected, response.Trim());
}

[Fact]
public async Task ViewStart_IsDiscoveredForFilesOutsidePageRoot()
{
//Arrange
var newLine = Environment.NewLine;
var expected = $"Hello from _ViewStart at root{newLine}Hello from _ViewStart{newLine}Hello from page";

// Act
var response = await Client.GetStringAsync("/WithViewStart/ViewStartAtRoot");

// Assert
Assert.Equal(expected, response.Trim());
}

[Fact]
public async Task ViewImport_IsDiscoveredWhenRootDirectoryIsSpecified()
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -196,8 +196,7 @@ public void OnProvidersExecuting_CachesViewStartFactories()
loader.Object,
CreateActionDescriptorCollection(descriptor),
razorPageFactoryProvider: razorPageFactoryProvider.Object,
razorProject: defaultRazorProject,
razorPagesOptions: new RazorPagesOptions { RootDirectory = "/" });
razorProject: defaultRazorProject);

var context = new ActionInvokerProviderContext(new ActionContext()
{
Expand Down Expand Up @@ -317,67 +316,7 @@ public void OnProvidersExecuting_UpdatesEntriesWhenActionDescriptorProviderColle
[Fact]
public void GetViewStartFactories_FindsFullHeirarchy()
{
// Arrange
var descriptor = new PageActionDescriptor()
{
RelativePath = "/Views/Deeper/Index.cshtml",
FilterDescriptors = new FilterDescriptor[0],
ViewEnginePath = "/Views/Deeper/Index.cshtml"
};

var loader = new Mock<IPageLoader>();
loader
.Setup(l => l.Load(It.IsAny<PageActionDescriptor>()))
.Returns(CreateCompiledPageActionDescriptor(descriptor, typeof(TestPageModel)));

var fileProvider = new TestFileProvider();
fileProvider.AddFile("/View/Deeper/Not_ViewStart.cshtml", "page content");
fileProvider.AddFile("/View/Wrong/_ViewStart.cshtml", "page content");
fileProvider.AddFile("/_ViewStart.cshtml", "page content ");
fileProvider.AddFile("/Views/_ViewStart.cshtml", "@page starts!");
fileProvider.AddFile("/Views/Deeper/_ViewStart.cshtml", "page content");

var razorProject = new TestRazorProject(fileProvider);

var mock = new Mock<IRazorPageFactoryProvider>();
mock
.Setup(p => p.CreateFactory("/Views/Deeper/_ViewStart.cshtml"))
.Returns(new RazorPageFactoryResult(() => null, new List<IChangeToken>()))
.Verifiable();
mock
.Setup(p => p.CreateFactory("/Views/_ViewStart.cshtml"))
.Returns(new RazorPageFactoryResult(() => null, new List<IChangeToken>()))
.Verifiable();
mock
.Setup(p => p.CreateFactory("/_ViewStart.cshtml"))
.Returns(new RazorPageFactoryResult(() => null, new List<IChangeToken>()))
.Verifiable();

var razorPageFactoryProvider = mock.Object;

var invokerProvider = CreateInvokerProvider(
loader.Object,
CreateActionDescriptorCollection(descriptor),
pageProvider: null,
modelProvider: null,
razorPageFactoryProvider: razorPageFactoryProvider,
razorProject: razorProject,
razorPagesOptions: new RazorPagesOptions { RootDirectory = "/" });

var compiledDescriptor = CreateCompiledPageActionDescriptor(descriptor);

// Act
var factories = invokerProvider.GetViewStartFactories(compiledDescriptor);

// Assert
mock.Verify();
}

[Theory]
[InlineData("/Pages/Level1/")]
[InlineData("/Pages/Level1")]
public void GetPageFactories_DoesNotFindViewStartsOutsideBaseDirectory(string rootDirectory)
{
// Arrange
var descriptor = new PageActionDescriptor()
{
Expand Down Expand Up @@ -414,19 +353,22 @@ public void GetPageFactories_DoesNotFindViewStartsOutsideBaseDirectory(string ro
.Setup(p => p.CreateFactory("/Pages/Level1/_ViewStart.cshtml"))
.Returns(new RazorPageFactoryResult(() => null, new List<IChangeToken>()))
.Verifiable();
var razorPageFactoryProvider = mock.Object;
mock
.Setup(p => p.CreateFactory("/Pages/_ViewStart.cshtml"))
.Returns(new RazorPageFactoryResult(() => null, new List<IChangeToken>()))
.Verifiable();
mock
.Setup(p => p.CreateFactory("/_ViewStart.cshtml"))
.Returns(new RazorPageFactoryResult(() => null, new List<IChangeToken>()))
.Verifiable();

var options = new RazorPagesOptions
{
RootDirectory = rootDirectory,
};
var razorPageFactoryProvider = mock.Object;

var invokerProvider = CreateInvokerProvider(
loader.Object,
CreateActionDescriptorCollection(descriptor),
razorPageFactoryProvider: razorPageFactoryProvider,
razorProject: razorProject,
razorPagesOptions: options);
razorProject: razorProject);

// Act
var factories = invokerProvider.GetViewStartFactories(compiledPageDescriptor);
Expand Down Expand Up @@ -474,8 +416,7 @@ public void GetViewStartFactories_ReturnsFactoriesForFilesThatDoNotExistInProjec
pageProvider: null,
modelProvider: null,
razorPageFactoryProvider: pageFactory.Object,
razorProject: razorProject,
razorPagesOptions: new RazorPagesOptions { RootDirectory = "/" });
razorProject: razorProject);

var compiledDescriptor = CreateCompiledPageActionDescriptor(descriptor);

Expand Down Expand Up @@ -513,8 +454,7 @@ private static PageActionInvokerProvider CreateInvokerProvider(
IPageFactoryProvider pageProvider = null,
IPageModelFactoryProvider modelProvider = null,
IRazorPageFactoryProvider razorPageFactoryProvider = null,
RazorProject razorProject = null,
RazorPagesOptions razorPagesOptions = null)
RazorProject razorProject = null)
{
var tempDataFactory = new Mock<ITempDataDictionaryFactory>();
tempDataFactory
Expand Down Expand Up @@ -544,7 +484,6 @@ private static PageActionInvokerProvider CreateInvokerProvider(
tempDataFactory.Object,
new TestOptionsManager<MvcOptions>(),
new TestOptionsManager<HtmlHelperOptions>(),
new TestOptionsManager<RazorPagesOptions>(razorPagesOptions ?? new RazorPagesOptions()),
Mock.Of<IPageHandlerMethodSelector>(),
razorProject,
new DiagnosticListener("Microsoft.AspNetCore"),
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
@page
Hello from page
6 changes: 6 additions & 0 deletions test/WebSites/RazorPagesWebSite/_ViewStart.cshtml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
@using Microsoft.AspNetCore.Mvc.RazorPages
@if (((PageActionDescriptor)ViewContext.ActionDescriptor).ViewEnginePath == "/WithViewStart/ViewStartAtRoot")
{
<text>Hello from _ViewStart at root
</text>
}

0 comments on commit aa5a348

Please sign in to comment.