Require cshtml ending for full path view names.

If a view represents a full path and does not end in .cshtml the FindView and FindPartialView methods will throw.

#206
This commit is contained in:
N. Taylor Mullen 2014-04-30 16:32:58 -07:00
parent f5b3ae4a3b
commit 3e398be1f8
3 changed files with 37 additions and 5 deletions

View File

@ -203,6 +203,22 @@ namespace Microsoft.AspNet.Mvc.Razor
return string.Format(CultureInfo.CurrentCulture, GetString("SectionsNotRendered"), p0);
}
/// <summary>
/// View '{0}' must have extension '{1}' when the view represents a full path.
/// </summary>
internal static string ViewMustEndInExtension
{
get { return GetString("ViewMustEndInExtension"); }
}
/// <summary>
/// View '{0}' must have extension '{1}' when the view represents a full path.
/// </summary>
internal static string FormatViewMustEndInExtension(object p0, object p1)
{
return string.Format(CultureInfo.CurrentCulture, GetString("ViewMustEndInExtension"), p0, p1);
}
/// <summary>
/// The method '{0}' cannot be invoked by this view.
/// </summary>

View File

@ -150,6 +150,9 @@
<data name="SectionsNotRendered" xml:space="preserve">
<value>The following sections have been defined but have not been rendered: '{0}'.</value>
</data>
<data name="ViewMustEndInExtension" xml:space="preserve">
<value>View '{0}' must have extension '{1}' when the view represents a full path.</value>
</data>
<data name="View_MethodCannotBeCalled" xml:space="preserve">
<value>The method '{0}' cannot be invoked by this view.</value>
</data>

View File

@ -25,17 +25,19 @@ namespace Microsoft.AspNet.Mvc.Razor
{
public class RazorViewEngine : IViewEngine
{
private static readonly string _viewExtension = ".cshtml";
private static readonly string[] _viewLocationFormats =
{
"/Views/{1}/{0}.cshtml",
"/Views/Shared/{0}.cshtml",
"/Views/{1}/{0}" + _viewExtension,
"/Views/Shared/{0}" + _viewExtension,
};
private static readonly string[] _areaViewLocationFormats =
{
"/Areas/{2}/Views/{1}/{0}.cshtml",
"/Areas/{2}/Views/Shared/{0}.cshtml",
"/Views/Shared/{0}.cshtml",
"/Areas/{2}/Views/{1}/{0}" + _viewExtension,
"/Areas/{2}/Views/Shared/{0}" + _viewExtension,
"/Views/Shared/{0}" + _viewExtension,
};
private readonly IVirtualPathViewFactory _virtualPathFactory;
@ -70,6 +72,8 @@ namespace Microsoft.AspNet.Mvc.Razor
if (nameRepresentsPath)
{
EnsureFullPathViewExtension(viewName);
var view = _virtualPathFactory.CreateInstance(viewName);
return view != null ? ViewEngineResult.Found(viewName, view) :
ViewEngineResult.NotFound(viewName, new[] { viewName });
@ -93,6 +97,15 @@ 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)
{
char c = name[0];