Addressed code review comments.
This commit is contained in:
parent
c986c663e3
commit
4494f40d67
|
|
@ -25,19 +25,19 @@ namespace Microsoft.AspNet.Mvc.Razor
|
||||||
{
|
{
|
||||||
public class RazorViewEngine : IViewEngine
|
public class RazorViewEngine : IViewEngine
|
||||||
{
|
{
|
||||||
private static readonly string _viewExtension = ".cshtml";
|
private const string ViewExtension = ".cshtml";
|
||||||
|
|
||||||
private static readonly string[] _viewLocationFormats =
|
private static readonly string[] _viewLocationFormats =
|
||||||
{
|
{
|
||||||
"/Views/{1}/{0}" + _viewExtension,
|
"/Views/{1}/{0}" + ViewExtension,
|
||||||
"/Views/Shared/{0}" + _viewExtension,
|
"/Views/Shared/{0}" + ViewExtension,
|
||||||
};
|
};
|
||||||
|
|
||||||
private static readonly string[] _areaViewLocationFormats =
|
private static readonly string[] _areaViewLocationFormats =
|
||||||
{
|
{
|
||||||
"/Areas/{2}/Views/{1}/{0}" + _viewExtension,
|
"/Areas/{2}/Views/{1}/{0}" + ViewExtension,
|
||||||
"/Areas/{2}/Views/Shared/{0}" + _viewExtension,
|
"/Areas/{2}/Views/Shared/{0}" + ViewExtension,
|
||||||
"/Views/Shared/{0}" + _viewExtension,
|
"/Views/Shared/{0}" + ViewExtension,
|
||||||
};
|
};
|
||||||
|
|
||||||
private readonly IVirtualPathViewFactory _virtualPathFactory;
|
private readonly IVirtualPathViewFactory _virtualPathFactory;
|
||||||
|
|
@ -72,7 +72,11 @@ namespace Microsoft.AspNet.Mvc.Razor
|
||||||
|
|
||||||
if (nameRepresentsPath)
|
if (nameRepresentsPath)
|
||||||
{
|
{
|
||||||
EnsureFullPathViewExtension(viewName);
|
if (!viewName.EndsWith(ViewExtension, StringComparison.OrdinalIgnoreCase))
|
||||||
|
{
|
||||||
|
throw new InvalidOperationException(
|
||||||
|
Resources.FormatViewMustEndInExtension(viewName, ViewExtension));
|
||||||
|
}
|
||||||
|
|
||||||
var view = _virtualPathFactory.CreateInstance(viewName);
|
var view = _virtualPathFactory.CreateInstance(viewName);
|
||||||
return view != null ? ViewEngineResult.Found(viewName, view) :
|
return view != null ? ViewEngineResult.Found(viewName, view) :
|
||||||
|
|
@ -97,15 +101,6 @@ namespace Microsoft.AspNet.Mvc.Razor
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private static void EnsureFullPathViewExtension(string viewName)
|
|
||||||
{
|
|
||||||
if(!viewName.EndsWith(_viewExtension))
|
|
||||||
{
|
|
||||||
throw new InvalidOperationException(
|
|
||||||
Resources.FormatViewMustEndInExtension(viewName, _viewExtension));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
private static bool IsSpecificPath(string name)
|
private static bool IsSpecificPath(string name)
|
||||||
{
|
{
|
||||||
char c = name[0];
|
char c = name[0];
|
||||||
|
|
|
||||||
|
|
@ -36,12 +36,20 @@ namespace Microsoft.AspNet.Mvc.Razor.Test
|
||||||
{"controller", "bar"},
|
{"controller", "bar"},
|
||||||
};
|
};
|
||||||
|
|
||||||
|
public static IEnumerable<string[]> InvalidViewNameValues
|
||||||
|
{
|
||||||
|
get
|
||||||
|
{
|
||||||
|
yield return new[] { "~/foo/bar" };
|
||||||
|
yield return new[] { "/foo/bar" };
|
||||||
|
yield return new[] { "~/foo/bar.txt" };
|
||||||
|
yield return new[] { "/foo/bar.txt" };
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
[Theory]
|
[Theory]
|
||||||
[InlineData("~/foo/bar")]
|
[MemberData("InvalidViewNameValues")]
|
||||||
[InlineData("/foo/bar")]
|
public void FindViewFullPathFailsWithNoCshtmlEnding(string viewName)
|
||||||
[InlineData("~/foo/bar.txt")]
|
|
||||||
[InlineData("/foo/bar.txt")]
|
|
||||||
public void FindViewFullPathRequiresCshtmlEnding(string viewName)
|
|
||||||
{
|
{
|
||||||
// Arrange
|
// Arrange
|
||||||
var viewEngine = CreateSearchLocationViewEngineTester();
|
var viewEngine = CreateSearchLocationViewEngineTester();
|
||||||
|
|
@ -49,10 +57,18 @@ namespace Microsoft.AspNet.Mvc.Razor.Test
|
||||||
// Act & Assert
|
// Act & Assert
|
||||||
Assert.Throws<InvalidOperationException>(() =>
|
Assert.Throws<InvalidOperationException>(() =>
|
||||||
viewEngine.FindView(_controllerTestContext, viewName));
|
viewEngine.FindView(_controllerTestContext, viewName));
|
||||||
|
}
|
||||||
|
|
||||||
// Append .cshtml so we can try and no longer throw.
|
[Theory]
|
||||||
|
[MemberData("InvalidViewNameValues")]
|
||||||
|
public void FindViewFullPathSucceedsWithCshtmlEnding(string viewName)
|
||||||
|
{
|
||||||
|
// Arrange
|
||||||
|
var viewEngine = CreateSearchLocationViewEngineTester();
|
||||||
|
// Append .cshtml so the viewname is no longer invalid
|
||||||
viewName += ".cshtml";
|
viewName += ".cshtml";
|
||||||
|
|
||||||
|
// Act & Assert
|
||||||
// If this throws then our test case fails
|
// If this throws then our test case fails
|
||||||
var result = viewEngine.FindPartialView(_controllerTestContext, viewName);
|
var result = viewEngine.FindPartialView(_controllerTestContext, viewName);
|
||||||
|
|
||||||
|
|
@ -60,22 +76,27 @@ namespace Microsoft.AspNet.Mvc.Razor.Test
|
||||||
}
|
}
|
||||||
|
|
||||||
[Theory]
|
[Theory]
|
||||||
[InlineData("~/foo/bar")]
|
[MemberData("InvalidViewNameValues")]
|
||||||
[InlineData("/foo/bar")]
|
public void FindPartialViewFullPathFailsWithNoCshtmlEnding(string partialViewName)
|
||||||
[InlineData("~/foo/bar.txt")]
|
|
||||||
[InlineData("/foo/bar.txt")]
|
|
||||||
public void FindPartialViewFullPathRequiresCshtmlEnding(string partialViewName)
|
|
||||||
{
|
{
|
||||||
// Arrange
|
// Arrange
|
||||||
var viewEngine = CreateSearchLocationViewEngineTester();
|
var viewEngine = CreateSearchLocationViewEngineTester();
|
||||||
|
|
||||||
// Act & Assert
|
// Act & Assert
|
||||||
Assert.Throws<InvalidOperationException>(() =>
|
Assert.Throws<InvalidOperationException>(() =>
|
||||||
viewEngine.FindPartialView(_controllerTestContext, partialViewName));
|
viewEngine.FindPartialView(_controllerTestContext, partialViewName));
|
||||||
|
}
|
||||||
|
|
||||||
// Append .cshtml so we can try and no longer throw.
|
[Theory]
|
||||||
|
[MemberData("InvalidViewNameValues")]
|
||||||
|
public void FindPartialViewFullPathSucceedsWithCshtmlEnding(string partialViewName)
|
||||||
|
{
|
||||||
|
// Arrange
|
||||||
|
var viewEngine = CreateSearchLocationViewEngineTester();
|
||||||
|
// Append .cshtml so the viewname is no longer invalid
|
||||||
partialViewName += ".cshtml";
|
partialViewName += ".cshtml";
|
||||||
|
|
||||||
|
// Act & Assert
|
||||||
// If this throws then our test case fails
|
// If this throws then our test case fails
|
||||||
var result = viewEngine.FindPartialView(_controllerTestContext, partialViewName);
|
var result = viewEngine.FindPartialView(_controllerTestContext, partialViewName);
|
||||||
|
|
||||||
|
|
@ -91,7 +112,7 @@ namespace Microsoft.AspNet.Mvc.Razor.Test
|
||||||
|
|
||||||
// Act
|
// Act
|
||||||
var result = viewEngine.FindPartialView(_areaTestContext, "partial");
|
var result = viewEngine.FindPartialView(_areaTestContext, "partial");
|
||||||
|
|
||||||
// Assert
|
// Assert
|
||||||
Assert.False(result.Success);
|
Assert.False(result.Success);
|
||||||
Assert.Equal(new[] {
|
Assert.Equal(new[] {
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue