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

Do not override default Layout value #3766

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 11 additions & 4 deletions src/Microsoft.AspNet.Mvc.Razor/RazorView.cs
Original file line number Diff line number Diff line change
Expand Up @@ -155,8 +155,12 @@ private async Task RenderViewStartsAsync(ViewContext context)
var viewStart = ViewStartPages[i];
context.ExecutingFilePath = viewStart.Path;

// Copy the layout value from the previous view start (if any) to the current.
viewStart.Layout = layout;
// If non-null, copy the layout value from the previous view start to the current. Otherwise leave
// Layout default alone.
if (layout != null)
{
viewStart.Layout = layout;
}

await RenderPageCoreAsync(viewStart, context);

Expand All @@ -170,8 +174,11 @@ private async Task RenderViewStartsAsync(ViewContext context)
context.ExecutingFilePath = oldFilePath;
}

// Copy the layout value from the view start page(s) (if any) to the entry page.
RazorPage.Layout = layout;
// If non-null, copy the layout value from the view start page(s) to the entry page.
if (layout != null)
{
RazorPage.Layout = layout;
}
}

private async Task RenderLayoutAsync(
Expand Down
97 changes: 96 additions & 1 deletion test/Microsoft.AspNet.Mvc.Razor.Test/RazorViewTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
using System.IO;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNet.Http.Features;
using Microsoft.AspNet.Http.Internal;
using Microsoft.AspNet.Mvc.Abstractions;
using Microsoft.AspNet.Mvc.ModelBinding;
Expand Down Expand Up @@ -359,6 +358,102 @@ public async Task RenderAsync_ExecutesViewStart()
activator.Verify();
}

[Fact]
public async Task RenderAsync_ExecutesDefaultLayout()
{
// Arrange
var path = "/Views/Home/Index.cshtml";
var layoutPath = "/Views/_Shared/_Layout.cshtml";
var page = new TestableRazorPage(p => { })
{
Path = path,
// Initialize Layout property when instantiated.
Layout = layoutPath,
};
var layoutExecuted = false;
var layout = new TestableRazorPage(
p =>
{
layoutExecuted = true;
p.RenderBodyPublic();
})
{
Path = layoutPath,
};

var viewEngine = new Mock<IRazorViewEngine>(MockBehavior.Strict);
viewEngine
.Setup(engine => engine.GetPage(path, layoutPath))
.Returns(new RazorPageResult(layoutPath, layout));

var view = new RazorView(
viewEngine.Object,
Mock.Of<IRazorPageActivator>(),
new IRazorPage[0],
page,
new HtmlTestEncoder());
var context = CreateViewContext(view);

// Act
await view.RenderAsync(context);

// Assert
Assert.True(layoutExecuted);
}

[Fact]
public async Task RenderAsync_ExecutesDefaultLayout_WithViewStart()
{
// Arrange
var path = "/Views/Home/Index.cshtml";
var layoutPath = "/Views/_Shared/_Layout.cshtml";
var viewStartPath = "/Views/_ViewStart.cshtml";

var viewStart = new TestableRazorPage(p => { })
{
Path = viewStartPath,
};
var page = new TestableRazorPage(p => { })
{
Path = path,
// Initialize Layout property when instantiated.
Layout = layoutPath,
};

var layoutExecuted = false;
var layout = new TestableRazorPage(
p =>
{
layoutExecuted = true;
p.RenderBodyPublic();
})
{
Path = layoutPath,
};

var viewEngine = new Mock<IRazorViewEngine>(MockBehavior.Strict);
viewEngine
.Setup(engine => engine.GetAbsolutePath(viewStartPath, /* pagePath */ null))
.Returns<string>(null);
viewEngine
.Setup(engine => engine.GetPage(path, layoutPath))
.Returns(new RazorPageResult(layoutPath, layout));

var view = new RazorView(
viewEngine.Object,
Mock.Of<IRazorPageActivator>(),
new[] { viewStart },
page,
new HtmlTestEncoder());
var context = CreateViewContext(view);

// Act
await view.RenderAsync(context);

// Assert
Assert.True(layoutExecuted);
}

[Fact]
public async Task RenderAsync_ThrowsIfLayoutPageCannotBeFound_MessageUsesGetPageLocations()
{
Expand Down