diff --git a/src/Nancy.Tests.Functional/Modules/RazorTestModule.cs b/src/Nancy.Tests.Functional/Modules/RazorTestModule.cs index d4d14049c4..cd76344e32 100644 --- a/src/Nancy.Tests.Functional/Modules/RazorTestModule.cs +++ b/src/Nancy.Tests.Functional/Modules/RazorTestModule.cs @@ -24,6 +24,13 @@ public RazorTestModule() return serialized; }; + + Get["/razor-partialnotfound"] = _ => + { + this.ViewBag.Name = "Bob"; + + return View["RazorPageWithUnknownPartial"]; + }; } } } diff --git a/src/Nancy.Tests.Functional/Nancy.Tests.Functional.csproj b/src/Nancy.Tests.Functional/Nancy.Tests.Functional.csproj index 159d59703f..d6be718f42 100644 --- a/src/Nancy.Tests.Functional/Nancy.Tests.Functional.csproj +++ b/src/Nancy.Tests.Functional/Nancy.Tests.Functional.csproj @@ -155,6 +155,9 @@ + + Always + Always diff --git a/src/Nancy.Tests.Functional/Tests/PartialViewTests.cs b/src/Nancy.Tests.Functional/Tests/PartialViewTests.cs index 98c2de7f78..0c8f421de6 100644 --- a/src/Nancy.Tests.Functional/Tests/PartialViewTests.cs +++ b/src/Nancy.Tests.Functional/Tests/PartialViewTests.cs @@ -1,9 +1,10 @@ namespace Nancy.Tests.Functional.Tests { using System; - using Bootstrapper; - using Modules; - using Testing; + using Nancy.Bootstrapper; + using Nancy.Testing; + using Nancy.Tests.Functional.Modules; + using Nancy.ViewEngines; using Xunit; public class PartialViewTests @@ -41,5 +42,16 @@ public void When_Using_Partial_View_Then_First_Index_Of_ViewStart_Should_Equal_L // If the index is not the same then the string occurs twice... Assert.Equal(firstIndex, lastIndex); } + + [Fact] + public void When_Partial_View_Could_Not_Be_Found_An_Meaningful_Exception_Should_Be_Thrown() + { + Assert.Throws(() => + { + var response = this.browser.Get(@"/razor-partialnotfound"); + + response.Body.AsString(); + }); + } } } diff --git a/src/Nancy.Tests.Functional/Views/RazorPageWithUnknownPartial.cshtml b/src/Nancy.Tests.Functional/Views/RazorPageWithUnknownPartial.cshtml new file mode 100644 index 0000000000..ddfa3e83c0 --- /dev/null +++ b/src/Nancy.Tests.Functional/Views/RazorPageWithUnknownPartial.cshtml @@ -0,0 +1,5 @@ +@inherits Nancy.ViewEngines.Razor.NancyRazorViewBase + +Hello @ViewBag.Name + +@Html.Partial("_UnknownPartialTest.cshtml") \ No newline at end of file diff --git a/src/Nancy.ViewEngines.Razor/HtmlHelpers.cs b/src/Nancy.ViewEngines.Razor/HtmlHelpers.cs index 3d33d92d27..03e9e380c1 100644 --- a/src/Nancy.ViewEngines.Razor/HtmlHelpers.cs +++ b/src/Nancy.ViewEngines.Razor/HtmlHelpers.cs @@ -2,7 +2,8 @@ { using System; using System.IO; - using Nancy.Security; + using System.Linq; + using System.Security.Claims; /// /// Helpers to generate html content. @@ -76,6 +77,10 @@ public IHtmlString Partial(string viewName, dynamic modelForPartial) { var view = this.RenderContext.LocateView(viewName, modelForPartial); + if (view == null) { + throw new ViewNotFoundException(viewName, Engine.Extensions.ToArray()); + } + var response = this.Engine.RenderView(view, modelForPartial, this.RenderContext, true); Action action = response.Contents; var mem = new MemoryStream(); diff --git a/src/Nancy/ViewEngines/ViewNotFoundException.cs b/src/Nancy/ViewEngines/ViewNotFoundException.cs index 50a667bb00..00322f3498 100644 --- a/src/Nancy/ViewEngines/ViewNotFoundException.cs +++ b/src/Nancy/ViewEngines/ViewNotFoundException.cs @@ -39,6 +39,23 @@ public ViewNotFoundException(string viewName, string[] availableViewEngineExtens Environment.NewLine); } + /// + /// Initializes a new instance of the . + /// + /// The name of the view that was being located. + /// List of available view extensions that can be rendered by the available view engines. + public ViewNotFoundException(string viewName, string[] availableViewEngineExtensions) + { + this.ViewName = viewName; + this.AvailableViewEngineExtensions = availableViewEngineExtensions; + + this.message = String.Format( + "Unable to locate view '{0}'{2}Currently available view engine extensions: {1}{2}", + this.ViewName, + string.Join(",", this.AvailableViewEngineExtensions), + Environment.NewLine); + } + /// /// Initializes a new instance of the ///