diff --git a/src/Microsoft.AspNet.Mvc.Razor/IRazorPage.cs b/src/Microsoft.AspNet.Mvc.Razor/IRazorPage.cs index db57d73146..ec65dc579b 100644 --- a/src/Microsoft.AspNet.Mvc.Razor/IRazorPage.cs +++ b/src/Microsoft.AspNet.Mvc.Razor/IRazorPage.cs @@ -44,6 +44,12 @@ namespace Microsoft.AspNet.Mvc.Razor /// string Layout { get; set; } + /// + /// Gets or sets a value that determines if the current instance of is being executed + /// from a partial view. + /// + bool IsPartial { get; set; } + /// /// Gets or sets a instance used to instrument the page execution. /// diff --git a/src/Microsoft.AspNet.Mvc.Razor/RazorPage.cs b/src/Microsoft.AspNet.Mvc.Razor/RazorPage.cs index 6bf5fd5a88..2db0e165da 100644 --- a/src/Microsoft.AspNet.Mvc.Razor/RazorPage.cs +++ b/src/Microsoft.AspNet.Mvc.Razor/RazorPage.cs @@ -57,6 +57,9 @@ namespace Microsoft.AspNet.Mvc.Razor /// public string Layout { get; set; } + /// + public bool IsPartial { get; set; } + /// public IPageExecutionContext PageExecutionContext { get; set; } diff --git a/src/Microsoft.AspNet.Mvc.Razor/RazorView.cs b/src/Microsoft.AspNet.Mvc.Razor/RazorView.cs index 2631b9cdc0..653216ed45 100644 --- a/src/Microsoft.AspNet.Mvc.Razor/RazorView.cs +++ b/src/Microsoft.AspNet.Mvc.Razor/RazorView.cs @@ -136,6 +136,7 @@ namespace Microsoft.AspNet.Mvc.Razor private async Task RenderPageCoreAsync(IRazorPage page, ViewContext context) { + page.IsPartial = _isPartial; page.ViewContext = context; if (EnableInstrumentation) { diff --git a/test/Microsoft.AspNet.Mvc.Razor.Test/RazorViewTest.cs b/test/Microsoft.AspNet.Mvc.Razor.Test/RazorViewTest.cs index 72f329ca88..be538c97f7 100644 --- a/test/Microsoft.AspNet.Mvc.Razor.Test/RazorViewTest.cs +++ b/test/Microsoft.AspNet.Mvc.Razor.Test/RazorViewTest.cs @@ -788,6 +788,69 @@ section-content-2"; Assert.Equal(expected, actual); } + [Fact] + public async Task IsPartial_IsSetToFalse_ForViewStartPageAndLayoutOfAView() + { + // Arrange + bool? isPartialPage = null; + bool? isPartialLayout = null; + bool? isPartialViewStart = null; + + var page = new TestableRazorPage(v => + { + isPartialPage = v.IsPartial; + }); + var viewStart = new TestableRazorPage(v => + { + v.Layout = "/Layout.cshtml"; + isPartialViewStart = v.IsPartial; + }); + var layout = new TestableRazorPage(v => + { + isPartialLayout = v.IsPartial; + v.RenderBodyPublic(); + }); + var pageFactory = new Mock(); + pageFactory.Setup(p => p.CreateInstance("/Layout.cshtml")) + .Returns(layout); + + var view = new RazorView(pageFactory.Object, + Mock.Of(), + CreateViewStartProvider(viewStart)); + view.Contextualize(page, isPartial: false); + var viewContext = CreateViewContext(view); + + // Act + await view.RenderAsync(viewContext); + + // Assert + Assert.False(isPartialPage.Value); + Assert.False(isPartialLayout.Value); + Assert.False(isPartialViewStart.Value); + } + + [Fact] + public async Task IsPartial_IsSetToTrue_ForPartialView() + { + // Arrange + bool? isPartialPage = null; + var page = new TestableRazorPage(v => + { + isPartialPage = v.IsPartial; + }); + var view = new RazorView(Mock.Of(), + Mock.Of(), + CreateViewStartProvider()); + view.Contextualize(page, isPartial: true); + var viewContext = CreateViewContext(view); + + // Act + await view.RenderAsync(viewContext); + + // Assert + Assert.True(isPartialPage.Value); + } + private static TextWriter CreateBufferedWriter() { var mockWriter = new Mock();