Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

A couple of minor changes to the view location providers and defaultv…

…iewlocator.. also added Leo Duran to the list of contributors
  • Loading branch information...
commit c9f9913e8fcf6c0d6efc2c11a10035cde4166da2 1 parent 23a85a6
@thecodejunkie thecodejunkie authored
View
1  .gitignore
@@ -17,3 +17,4 @@ build/
[Rr]elease/
_[Rr]e[Ss]harper.*/
*.docstates
+*.tss
View
5 readme.md
@@ -108,7 +108,7 @@ Nancy is not a one man project and many of the features that are availble would
* [Andy Pike](http://github.com/andypike)
* [Bjarte Djuvik Næss](http://github.com/bjartn)
-* [Carlo Kok](https://github.com/carlokok)
+* [Carlo Kok](http://github.com/carlokok)
* [Chris Nicola](http://github.com/lucisferre)
* [David Hong](http://github.com/davidhong)
* [Graeme Foster](http://github.com/GraemeF)
@@ -117,13 +117,14 @@ Nancy is not a one man project and many of the features that are availble would
* [Jonas Cannehag](http://github.com/knecke)
* [José F. Romaniello](http://github.com/jfromaniello)
* [Karl Seguin](http://github.com/karlseguin)
+* [Leo Duran](http://github.com/leoduran)
* [Luke Smith](http://github.com/lukesmith)
* [James Eggers](http://github.com/jameseggers1)
* [Jason Mead](http://github.com/meadiagenic)
* [Jeremy Skinner](http://github.com/jeremyskinner)
* [João Bragança](http://github.com/thefringeninja)
* [Johan Danforth](http://github.com/johandanforth)
-* [Johan Nilsson](https://github.com/Dashue)
+* [Johan Nilsson](http://github.com/Dashue)
* [John Downey](http://github.com/jtdowney)
* [Mark Rendle](http://github.com/markrendle)
* [Maciej Kowalewski](http://github.com/maciejk)
View
10 src/Nancy.Demo.Hosting.Aspnet/MainModule.cs
@@ -70,16 +70,6 @@ public MainModule(IRouteCacheProvider routeCacheProvider)
return View["ssve.sshtml", model];
};
- Get["/embedded"] = x => {
- var model = new RatPack { FirstName = "Embedded" };
- return View["embedded", model];
- };
-
- Get["/embedded2"] = x => {
- var model = new RatPack { FirstName = "Embedded2" };
- return View["embedded.django", model];
- };
-
Get["/viewmodelconvention"] = x => {
return View[new SomeViewModel()];
};
View
4 src/Nancy.Demo.Hosting.Aspnet/Nancy.Demo.Hosting.Aspnet.csproj
@@ -100,7 +100,6 @@
<Content Include="Content\main.css" />
<Content Include="Content\scripts.js" />
<Content Include="Views\javascript.html" />
- <EmbeddedResource Include="Views\static-embedded.html" />
<Content Include="Views\static.htm" />
<Content Include="Web.config">
<SubType>Designer</SubType>
@@ -160,7 +159,6 @@
</ProjectReference>
</ItemGroup>
<ItemGroup>
- <EmbeddedResource Include="Views\embedded.django" />
<Content Include="Views\someview.cshtml" />
<Content Include="Views\razor-layout.cshtml" />
<None Include="Views\anon.spark" />
@@ -184,7 +182,7 @@
<UseIIS>False</UseIIS>
<AutoAssignPort>False</AutoAssignPort>
<DevelopmentServerPort>40965</DevelopmentServerPort>
- <DevelopmentServerVPath>/nancytest</DevelopmentServerVPath>
+ <DevelopmentServerVPath>/</DevelopmentServerVPath>
<IISUrl>http://localhost:59015/nancytest</IISUrl>
<NTLMAuthentication>False</NTLMAuthentication>
<UseCustomServer>False</UseCustomServer>
View
10 src/Nancy.Demo.Hosting.Aspnet/Views/embedded.django
@@ -1,10 +0,0 @@
-{% model Model:Nancy.Demo.Models.RatPack %}
-<html>
-<head>
- <title>{% block title %}Django View Engine Demo{% endblock title %}</title>
-</head>
-<body>
- <h1>Hello {{ Model.FirstName }}</h1>
- <p>This is a sample NDjango view!</p>
-</body>
-</html>
View
9 src/Nancy.Demo.Hosting.Aspnet/Views/static-embedded.html
@@ -1,9 +0,0 @@
-<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
-<html xmlns="http://www.w3.org/1999/xhtml">
-<head>
- <title></title>
-</head>
-<body>
-This is an embedded view
-</body>
-</html>
View
38 src/Nancy.Tests/Unit/ViewEngines/DefaultViewLocatorFixture.cs
@@ -1,7 +1,5 @@
namespace Nancy.Tests.Unit.ViewEngines
{
- using System;
-
using Fakes;
using Nancy.ViewEngines;
using Xunit;
@@ -20,8 +18,6 @@ public DefaultViewLocatorFixture()
this.viewLocator = CreateViewLocator(this.viewLocationCache);
}
- // Correct exception message when AmbiguousViewsException
-
[Fact]
public void Should_return_null_if_locate_view_is_invoked_with_null_view_name()
{
@@ -98,6 +94,40 @@ public void Should_throw_ambiguousviewsexception_when_locating_view_by_name_retu
}
[Fact]
+ public void Should_throw_ambiguousviewsexception_when_locating_view_by_name_and_multiple_views_share_the_same_name_and_location_but_different_extensions()
+ {
+ // Given
+ var expectedView1 = new ViewLocationResult(string.Empty, "index", "spark", () => null);
+ var expectedView2 = new ViewLocationResult(string.Empty, "index", "html", () => null);
+ var cache = new FakeViewLocationCache(expectedView1, expectedView2);
+ var locator = CreateViewLocator(cache);
+
+ // When
+ var exception = Record.Exception(() => locator.LocateView("index"));
+
+ // Then
+ exception.ShouldBeOfType<AmbiguousViewsException>();
+ }
+
+ [Fact]
+ public void Should_set_message_on_ambiguousviewexception()
+ {
+ // Given
+ var expectedView1 = new ViewLocationResult(string.Empty, "index", "spark", () => null);
+ var expectedView2 = new ViewLocationResult(string.Empty, "index", "html", () => null);
+ var cache = new FakeViewLocationCache(expectedView1, expectedView2);
+ var locator = CreateViewLocator(cache);
+
+ const string expectedMessage = "This exception was thrown because multiple views were found. 2 view(s):\r\n\t/index.spark\r\n\t/index.html";
+
+ // When
+ var exception = Record.Exception(() => locator.LocateView("index"));
+
+ // Then
+ exception.Message.ShouldEqual(expectedMessage);
+ }
+
+ [Fact]
public void Should_return_null_when_view_cannot_be_located_using_name()
{
// Given
View
15 src/Nancy/ViewEngines/AmbiguousViewsException.cs
@@ -7,5 +7,20 @@
/// </summary>
public class AmbiguousViewsException : Exception
{
+ /// <summary>
+ /// Initializes a new instance of the <see cref="AmbiguousViewsException"/> class.
+ /// </summary>
+ public AmbiguousViewsException()
+ {
+ }
+
+ /// <summary>
+ /// Initializes a new instance of the <see cref="AmbiguousViewsException"/> class.
+ /// </summary>
+ /// <param name="message">The message that should be displayed with the exception.</param>
+ public AmbiguousViewsException(string message)
+ : base(message)
+ {
+ }
}
}
View
19 src/Nancy/ViewEngines/DefaultViewLocator.cs
@@ -29,18 +29,29 @@ public ViewLocationResult LocateView(string viewName)
return null;
}
- var viewThatMatchCriteria = this.viewLocationCache
+ var viewsThatMatchesCritera = this.viewLocationCache
.Where(x => NameMatchesView(viewName, x))
.Where(x => ExtensionMatchesView(viewName, x))
.Where(x => LocationMatchesView(viewName, x))
.ToList();
- if (viewThatMatchCriteria.Count() > 1)
+ var count = viewsThatMatchesCritera.Count();
+ if (count > 1)
{
- throw new AmbiguousViewsException();
+ throw new AmbiguousViewsException(GetAmgiguousViewExceptionMessage(count, viewsThatMatchesCritera));
}
- return viewThatMatchCriteria.SingleOrDefault();
+ return viewsThatMatchesCritera.SingleOrDefault();
+ }
+
+ private static string GetAmgiguousViewExceptionMessage(int count, IEnumerable<ViewLocationResult> viewsThatMatchesCritera)
+ {
+ return string.Format("This exception was thrown because multiple views were found. {0} view(s):\r\n\t{1}", count, string.Join("\r\n\t", viewsThatMatchesCritera.Select(GetFullLocationOfView).ToArray()));
+ }
+
+ private static string GetFullLocationOfView(ViewLocationResult viewLocationResult)
+ {
+ return string.Concat(viewLocationResult.Location, "/", viewLocationResult.Name, ".", viewLocationResult.Extension);
}
private static bool ExtensionMatchesView(string viewName, ViewLocationResult viewLocationResult)
View
17 src/Nancy/ViewEngines/FileSystemViewLocationProvider.cs
@@ -9,7 +9,7 @@
/// </summary>
public class FileSystemViewLocationProvider : IViewLocationProvider
{
- private readonly IRootPathProvider rootPathProvider;
+ private readonly string rootPath;
/// <summary>
/// Initializes a new instance of the <see cref="FileSystemViewLocationProvider"/> class.
@@ -17,7 +17,7 @@ public class FileSystemViewLocationProvider : IViewLocationProvider
/// <param name="rootPathProvider">A <see cref="IRootPathProvider"/> instance..</param>
public FileSystemViewLocationProvider(IRootPathProvider rootPathProvider)
{
- this.rootPathProvider = rootPathProvider;
+ this.rootPath = rootPathProvider.GetRootPath();
}
/// <summary>
@@ -28,19 +28,24 @@ public FileSystemViewLocationProvider(IRootPathProvider rootPathProvider)
/// <remarks>If no views could be located, this method should return an empty enumerable, never <see langword="null"/>.</remarks>
public IEnumerable<ViewLocationResult> GetLocatedViews(IEnumerable<string> supportedViewExtensions)
{
+ if (string.IsNullOrEmpty(this.rootPath))
+ {
+ return Enumerable.Empty<ViewLocationResult>();
+ }
+
return
from match in GetViewsWithSupportedExtensions(supportedViewExtensions)
select new ViewLocationResult(
- GetViewLocation(match, this.rootPathProvider),
+ GetViewLocation(match, rootPath),
Path.GetFileNameWithoutExtension(match),
Path.GetExtension(match).Substring(1),
() => new StreamReader(new FileStream(match, FileMode.Open, FileAccess.Read, FileShare.ReadWrite)));
}
- private static string GetViewLocation(string match, IRootPathProvider rootPathProvider)
+ private static string GetViewLocation(string match, string rootPath)
{
var location = match
- .Replace(rootPathProvider.GetRootPath(), string.Empty)
+ .Replace(rootPath, string.Empty)
.TrimStart(new[] { Path.DirectorySeparatorChar })
.Replace(@"\", "/")
.Replace(Path.GetFileName(match), string.Empty)
@@ -52,7 +57,7 @@ private static string GetViewLocation(string match, IRootPathProvider rootPathPr
private IEnumerable<string> GetViewsWithSupportedExtensions(IEnumerable<string> supportedViewExtensions)
{
return supportedViewExtensions
- .SelectMany(extensions => Directory.GetFiles(this.rootPathProvider.GetRootPath(),
+ .SelectMany(extensions => Directory.GetFiles(this.rootPath,
string.Concat("*.", extensions),
SearchOption.AllDirectories)
).Distinct().ToList();
View
6 src/Nancy/ViewEngines/ResourceViewLocationProvider.cs
@@ -12,7 +12,7 @@
/// </summary>
public class ResourceViewLocationProvider : IViewLocationProvider
{
- public static IDictionary<Type, string> RootNamespaces = new Dictionary<Type, string>();
+ public static IDictionary<Assembly, string> RootNamespaces = new Dictionary<Assembly, string>();
/// <summary>
/// Initializes a new instance of the <see cref="ResourceViewLocationProvider"/> class.
@@ -63,8 +63,8 @@ private static IEnumerable<ViewLocationResult> GetViewLocations(Assembly assembl
return Enumerable.Empty<ViewLocationResult>();
}
- var commonNamespace = RootNamespaces.ContainsKey(assembly.GetType()) ?
- RootNamespaces[assembly.GetType()] :
+ var commonNamespace = RootNamespaces.ContainsKey(assembly) ?
+ RootNamespaces[assembly] :
ExtractAssemblyRootNamespace(assembly);
if (string.IsNullOrEmpty(commonNamespace))
Please sign in to comment.
Something went wrong with that request. Please try again.