diff --git a/src/Microsoft.AspNet.Mvc.Razor.Host/ViewStartUtility.cs b/src/Microsoft.AspNet.Mvc.Razor.Host/ViewStartUtility.cs index fd71c096d1..347e8aa428 100644 --- a/src/Microsoft.AspNet.Mvc.Razor.Host/ViewStartUtility.cs +++ b/src/Microsoft.AspNet.Mvc.Razor.Host/ViewStartUtility.cs @@ -47,6 +47,16 @@ namespace Microsoft.AspNet.Mvc.Razor path = path.Substring(1); } + if (string.Equals(ViewStartFileName, Path.GetFileName(path), StringComparison.OrdinalIgnoreCase)) + { + // If the specified path is a ViewStart file, then the first view start that applies to it is the + // parent view start. + if (!fileSystem.TryGetParentPath(path, out path)) + { + return Enumerable.Empty(); + } + } + var viewStartLocations = new List(); while (fileSystem.TryGetParentPath(path, out path)) diff --git a/test/Microsoft.AspNet.Mvc.Razor.Host.Test/TestFiles/ViewStartUtilityFiles/Areas/MyArea/Sub/Views/Admin/ViewStart.cshtml b/test/Microsoft.AspNet.Mvc.Razor.Host.Test/TestFiles/ViewStartUtilityFiles/Areas/MyArea/Sub/Views/Admin/ViewStart.cshtml new file mode 100644 index 0000000000..5f282702bb --- /dev/null +++ b/test/Microsoft.AspNet.Mvc.Razor.Host.Test/TestFiles/ViewStartUtilityFiles/Areas/MyArea/Sub/Views/Admin/ViewStart.cshtml @@ -0,0 +1 @@ + \ No newline at end of file diff --git a/test/Microsoft.AspNet.Mvc.Razor.Host.Test/TestFiles/ViewStartUtilityFiles/Areas/MyArea/Sub/Views/Admin/_viewstart.cshtml b/test/Microsoft.AspNet.Mvc.Razor.Host.Test/TestFiles/ViewStartUtilityFiles/Areas/MyArea/Sub/Views/Admin/_viewstart.cshtml new file mode 100644 index 0000000000..5f282702bb --- /dev/null +++ b/test/Microsoft.AspNet.Mvc.Razor.Host.Test/TestFiles/ViewStartUtilityFiles/Areas/MyArea/Sub/Views/Admin/_viewstart.cshtml @@ -0,0 +1 @@ + \ No newline at end of file diff --git a/test/Microsoft.AspNet.Mvc.Razor.Host.Test/TestFiles/ViewStartUtilityFiles/Views/Home/_viewstart.cshtml b/test/Microsoft.AspNet.Mvc.Razor.Host.Test/TestFiles/ViewStartUtilityFiles/Views/Home/_viewstart.cshtml new file mode 100644 index 0000000000..5f282702bb --- /dev/null +++ b/test/Microsoft.AspNet.Mvc.Razor.Host.Test/TestFiles/ViewStartUtilityFiles/Views/Home/_viewstart.cshtml @@ -0,0 +1 @@ + \ No newline at end of file diff --git a/test/Microsoft.AspNet.Mvc.Razor.Host.Test/TestFiles/ViewStartUtilityFiles/_viewstart.cshtml b/test/Microsoft.AspNet.Mvc.Razor.Host.Test/TestFiles/ViewStartUtilityFiles/_viewstart.cshtml new file mode 100644 index 0000000000..5f282702bb --- /dev/null +++ b/test/Microsoft.AspNet.Mvc.Razor.Host.Test/TestFiles/ViewStartUtilityFiles/_viewstart.cshtml @@ -0,0 +1 @@ + \ No newline at end of file diff --git a/test/Microsoft.AspNet.Mvc.Razor.Host.Test/ViewStartUtilityTest.cs b/test/Microsoft.AspNet.Mvc.Razor.Host.Test/ViewStartUtilityTest.cs index f8231c847b..aa730b50cd 100644 --- a/test/Microsoft.AspNet.Mvc.Razor.Host.Test/ViewStartUtilityTest.cs +++ b/test/Microsoft.AspNet.Mvc.Razor.Host.Test/ViewStartUtilityTest.cs @@ -45,8 +45,31 @@ namespace Microsoft.AspNet.Mvc.Razor Assert.Equal(expected, result); } - [Fact] - public void GetViewStartLocations_ReturnsPotentialViewStartLocations_IfPathIsAbsolute() + [Theory] + [InlineData("/views/Home/_ViewStart.cshtml")] + [InlineData("~/views/Home/_viewstart.cshtml")] + [InlineData("views/Home/_Viewstart.cshtml")] + public void GetViewStartLocations_SkipsCurrentPath_IfCurrentIsViewStart(string inputPath) + { + // Arrange + var expected = new[] + { + @"views\_viewstart.cshtml", + @"_viewstart.cshtml" + }; + var fileSystem = new PhysicalFileSystem(GetTestFileSystemBase()); + + // Act + var result = ViewStartUtility.GetViewStartLocations(fileSystem, inputPath); + + // Assert + Assert.Equal(expected, result); + } + + [Theory] + [InlineData("Test.cshtml")] + [InlineData("ViewStart.cshtml")] + public void GetViewStartLocations_ReturnsPotentialViewStartLocations_IfPathIsAbsolute(string fileName) { // Arrange var expected = new[] @@ -59,7 +82,7 @@ namespace Microsoft.AspNet.Mvc.Razor @"_viewstart.cshtml", }; var appBase = GetTestFileSystemBase(); - var viewPath = Path.Combine(appBase, "Areas", "MyArea", "Sub", "Views", "Admin", "Test.cshtml"); + var viewPath = Path.Combine(appBase, "Areas", "MyArea", "Sub", "Views", "Admin", fileName); var fileSystem = new PhysicalFileSystem(appBase); // Act @@ -69,6 +92,46 @@ namespace Microsoft.AspNet.Mvc.Razor Assert.Equal(expected, result); } + [Theory] + [InlineData("_ViewStart.cshtml")] + [InlineData("_viewstart.cshtml")] + public void GetViewStartLocations_SkipsCurrentPath_IfAbsolutePathIsAViewStartFile(string fileName) + { + // Arrange + var expected = new[] + { + @"Areas\MyArea\Sub\Views\_viewstart.cshtml", + @"Areas\MyArea\Sub\_viewstart.cshtml", + @"Areas\MyArea\_viewstart.cshtml", + @"Areas\_viewstart.cshtml", + @"_viewstart.cshtml", + }; + var appBase = GetTestFileSystemBase(); + var viewPath = Path.Combine(appBase, "Areas", "MyArea", "Sub", "Views", "Admin", fileName); + var fileSystem = new PhysicalFileSystem(appBase); + + // Act + var result = ViewStartUtility.GetViewStartLocations(fileSystem, viewPath); + + // Assert + Assert.Equal(expected, result); + } + + [Fact] + public void GetViewStartLocations_ReturnsEmptySequence_IfViewStartIsAtRoot() + { + // Arrange + var appBase = GetTestFileSystemBase(); + var viewPath = "_viewstart.cshtml"; + var fileSystem = new PhysicalFileSystem(appBase); + + // Act + var result = ViewStartUtility.GetViewStartLocations(fileSystem, viewPath); + + // Assert + Assert.Empty(result); + } + private static string GetTestFileSystemBase() { var serviceProvider = CallContextServiceLocator.Locator.ServiceProvider;