Skip to content
This repository has been archived by the owner on Jan 24, 2021. It is now read-only.

Commit

Permalink
Replaced an unintended NullReferenceException when a partial view cou…
Browse files Browse the repository at this point in the history
…ld not be located and introduced a ViewNotFoundException instead

Conflicts:
	src/Nancy.Tests.Functional/Tests/PartialViewTests.cs
	src/Nancy.ViewEngines.Razor/HtmlHelpers.cs
  • Loading branch information
dwonisch authored and khellang committed Dec 18, 2015
1 parent 0b0f632 commit 2b26073
Show file tree
Hide file tree
Showing 6 changed files with 53 additions and 4 deletions.
7 changes: 7 additions & 0 deletions src/Nancy.Tests.Functional/Modules/RazorTestModule.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,13 @@ public RazorTestModule()
return serialized;
};

Get["/razor-partialnotfound"] = _ =>
{
this.ViewBag.Name = "Bob";
return View["RazorPageWithUnknownPartial"];
};
}
}
}
3 changes: 3 additions & 0 deletions src/Nancy.Tests.Functional/Nancy.Tests.Functional.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,9 @@
</ItemGroup>
<ItemGroup>
<None Include="packages.config" />
<None Include="Views\RazorPageWithUnknownPartial.cshtml">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
<None Include="Views\_PartialTest.cshtml">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
Expand Down
18 changes: 15 additions & 3 deletions src/Nancy.Tests.Functional/Tests/PartialViewTests.cs
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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<ViewNotFoundException>(() =>
{
var response = this.browser.Get(@"/razor-partialnotfound");
response.Body.AsString();
});
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
@inherits Nancy.ViewEngines.Razor.NancyRazorViewBase<dynamic>

Hello @ViewBag.Name

@Html.Partial("_UnknownPartialTest.cshtml")
7 changes: 6 additions & 1 deletion src/Nancy.ViewEngines.Razor/HtmlHelpers.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@
{
using System;
using System.IO;
using Nancy.Security;
using System.Linq;
using System.Security.Claims;

/// <summary>
/// Helpers to generate html content.
Expand Down Expand Up @@ -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<Stream> action = response.Contents;
var mem = new MemoryStream();
Expand Down
17 changes: 17 additions & 0 deletions src/Nancy/ViewEngines/ViewNotFoundException.cs
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,23 @@ public ViewNotFoundException(string viewName, string[] availableViewEngineExtens
Environment.NewLine);
}

/// <summary>
/// Initializes a new instance of the <see cref="ViewNotFoundException"/>.
/// </summary>
/// <param name="viewName">The name of the view that was being located.</param>
/// <param name="availableViewEngineExtensions">List of available view extensions that can be rendered by the available view engines.</param>
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);
}

/// <summary>
/// Initializes a new instance of the <see cref="ViewNotFoundException"/>
/// </summary>
Expand Down

0 comments on commit 2b26073

Please sign in to comment.