diff --git a/src/Nancy.Tests/Unit/Conventions/DefaultViewLocationConventionsFixture.cs b/src/Nancy.Tests/Unit/Conventions/DefaultViewLocationConventionsFixture.cs index e7fe9dacc4..5de1bc34ae 100644 --- a/src/Nancy.Tests/Unit/Conventions/DefaultViewLocationConventionsFixture.cs +++ b/src/Nancy.Tests/Unit/Conventions/DefaultViewLocationConventionsFixture.cs @@ -122,7 +122,7 @@ public void Should_define_convention_that_returns_viewname() { // Given this.viewLocationConventions.Initialise(this.conventions); - var convention = this.conventions.ViewLocationConventions[5]; + var convention = this.conventions.ViewLocationConventions[7]; // When var result = convention.Invoke( @@ -139,7 +139,7 @@ public void Should_define_convention_that_returns_viewname_in_views_folder() { // Given this.viewLocationConventions.Initialise(this.conventions); - var convention = this.conventions.ViewLocationConventions[4]; + var convention = this.conventions.ViewLocationConventions[6]; // When var result = convention.Invoke( @@ -156,7 +156,7 @@ public void Should_define_convention_that_returns_viewname_in_modulepath_subfold { // Given this.viewLocationConventions.Initialise(this.conventions); - var convention = this.conventions.ViewLocationConventions[0]; + var convention = this.conventions.ViewLocationConventions[2]; // When var result = convention.Invoke( @@ -173,7 +173,7 @@ public void Should_define_convention_that_returns_viewname_in_modulepath_subfold { // Given this.viewLocationConventions.Initialise(this.conventions); - var convention = this.conventions.ViewLocationConventions[0]; + var convention = this.conventions.ViewLocationConventions[2]; // When var result = convention.Invoke( @@ -224,7 +224,7 @@ public void Should_define_convention_that_returns_viewname_in_modulepath_folder( { // Given this.viewLocationConventions.Initialise(this.conventions); - var convention = this.conventions.ViewLocationConventions[2]; + var convention = this.conventions.ViewLocationConventions[3]; // When var result = convention.Invoke( @@ -275,7 +275,7 @@ public void Should_define_convention_that_returns_viewname_in_modulepath_folder_ { // Given this.viewLocationConventions.Initialise(this.conventions); - var convention = this.conventions.ViewLocationConventions[2]; + var convention = this.conventions.ViewLocationConventions[3]; // When var result = convention.Invoke( @@ -292,7 +292,7 @@ public void Should_define_convention_that_returns_viewname_in_modulename_subfold { // Given this.viewLocationConventions.Initialise(this.conventions); - var convention = this.conventions.ViewLocationConventions[1]; + var convention = this.conventions.ViewLocationConventions[4]; // When var result = convention.Invoke( @@ -309,7 +309,7 @@ public void Should_define_convention_that_returns_viewname_in_modulename_folder( { // Given this.viewLocationConventions.Initialise(this.conventions); - var convention = this.conventions.ViewLocationConventions[3]; + var convention = this.conventions.ViewLocationConventions[5]; // When var result = convention.Invoke( @@ -320,5 +320,73 @@ public void Should_define_convention_that_returns_viewname_in_modulename_folder( // Then result.ShouldEqual("modulename/viewname"); } + + [Fact] + public void Should_define_convention_that_returns_viewname_in_modulename_folder_in_modulepath_folder() + { + // Given + this.viewLocationConventions.Initialise(this.conventions); + var convention = this.conventions.ViewLocationConventions[1]; + + // When + var result = convention.Invoke( + "viewname", + null, + new ViewLocationContext { ModuleName = "modulename", ModulePath = "modulepath" }); + + // Then + result.ShouldEqual("modulepath/modulename/viewname"); + } + + [Fact] + public void Should_define_convention_that_returns_viewname_in_modulename_folder_in_modulepath_folder_in_views_folder() + { + // Given + this.viewLocationConventions.Initialise(this.conventions); + var convention = this.conventions.ViewLocationConventions[0]; + + // When + var result = convention.Invoke( + "viewname", + null, + new ViewLocationContext { ModuleName = "modulename", ModulePath = "modulepath" }); + + // Then + result.ShouldEqual("views/modulepath/modulename/viewname"); + } + + [Fact] + public void Should_define_convention_that_returns_viewname_in_modulename_folder_in_modulepath_folder_when_modulepath_contains_leading_slash() + { + // Given + this.viewLocationConventions.Initialise(this.conventions); + var convention = this.conventions.ViewLocationConventions[1]; + + // When + var result = convention.Invoke( + "viewname", + null, + new ViewLocationContext { ModuleName = "modulename", ModulePath = "/modulepath" }); + + // Then + result.ShouldEqual("modulepath/modulename/viewname"); + } + + [Fact] + public void Should_define_convention_that_returns_viewname_in_modulename_folder_in_modulepath_folder_in_views_folder_when_modulepath_contains_leading_slash() + { + // Given + this.viewLocationConventions.Initialise(this.conventions); + var convention = this.conventions.ViewLocationConventions[0]; + + // When + var result = convention.Invoke( + "viewname", + null, + new ViewLocationContext { ModuleName = "modulename", ModulePath = "/modulepath" }); + + // Then + result.ShouldEqual("views/modulepath/modulename/viewname"); + } } } \ No newline at end of file diff --git a/src/Nancy/Conventions/DefaultViewLocationConventions.cs b/src/Nancy/Conventions/DefaultViewLocationConventions.cs index dea046851d..3cb73dd855 100644 --- a/src/Nancy/Conventions/DefaultViewLocationConventions.cs +++ b/src/Nancy/Conventions/DefaultViewLocationConventions.cs @@ -39,29 +39,59 @@ private static void ConfigureViewLocationConventions(NancyConventions convention { conventions.ViewLocationConventions = new List> { + // 0 Handles: views / *modulepath* / *modulename* / *viewname* (viewName, model, viewLocationContext) =>{ - return string.IsNullOrEmpty(viewLocationContext.ModulePath) ? string.Empty : string.Concat("views/", viewLocationContext.ModulePath.TrimStart(new[] {'/'}), "/", viewName); + if (string.IsNullOrEmpty(viewLocationContext.ModulePath)) + { + return string.Empty; + } + + var path = viewLocationContext.ModulePath.TrimStart(new[] { '/' }); + + return string.Concat("views/", path, "/", viewLocationContext.ModuleName, "/", viewName); }, - (viewName, model, viewLocationContext) => { - return string.Concat("views/", viewLocationContext.ModuleName, "/", viewName); + // 1 Handles: *modulepath* / *modulename* / *viewname* + (viewName, model, viewLocationContext) =>{ + if (string.IsNullOrEmpty(viewLocationContext.ModulePath)) + { + return string.Empty; + } + + var path = viewLocationContext.ModulePath.TrimStart(new[] { '/' }); + + return string.Concat(path, "/", viewLocationContext.ModuleName, "/", viewName); }, + // 2 Handles: views / *modulepath* / *viewname* + (viewName, model, viewLocationContext) =>{ + return string.IsNullOrEmpty(viewLocationContext.ModulePath) ? string.Empty : string.Concat("views/", viewLocationContext.ModulePath.TrimStart(new[] {'/'}), "/", viewName); + }, + + // 3 Handles: *modulepath* / *viewname* (viewName, model, viewLocationContext) =>{ return string.IsNullOrEmpty(viewLocationContext.ModulePath) ? string.Empty : string.Concat(viewLocationContext.ModulePath.TrimStart(new[] { '/' }), "/", viewName); }, + // 4 Handles: views / *modulename* / *viewname* + (viewName, model, viewLocationContext) => { + return string.Concat("views/", viewLocationContext.ModuleName, "/", viewName); + }, + + // 5 Handles: *modulename* / *viewname* (viewName, model, viewLocationContext) => { return string.Concat(viewLocationContext.ModuleName, "/", viewName); }, + // 6 Handles: views / *viewname* (viewName, model, viewLocationContext) => { return string.Concat("views/", viewName); }, + // 7 Handles: *viewname* (viewName, model, viewLocationContext) => { return viewName; - }, + } }; } }