Refactor ViewEngine to support partials.
This references WEBFX-95 and is the preliminary part to support Partial helpers.
This commit is contained in:
parent
2b70156cf4
commit
a5ed1157d5
|
|
@ -42,7 +42,7 @@ namespace Microsoft.AspNet.Mvc
|
|||
private async Task<IView> FindView([NotNull] IDictionary<string, object> context,[NotNull] string viewName)
|
||||
{
|
||||
var result = await _viewEngine.FindView(context, viewName);
|
||||
result.EnsureSuccess();
|
||||
|
||||
return result.View;
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -78,7 +78,7 @@ namespace Microsoft.AspNet.Mvc
|
|||
private async Task<IView> FindView([NotNull] IDictionary<string, object> context, [NotNull] string viewName)
|
||||
{
|
||||
var result = await _viewEngine.FindView(context, viewName);
|
||||
result.EnsureSuccess();
|
||||
|
||||
return result.View;
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -58,6 +58,38 @@ namespace Microsoft.AspNet.Mvc.Razor
|
|||
return GetString("RenderBodyCannotBeCalled");
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// The partial view '{0}' was not found. The following locations were searched:{1}
|
||||
/// </summary>
|
||||
internal static string ViewEngine_PartialViewNotFound
|
||||
{
|
||||
get { return GetString("ViewEngine_PartialViewNotFound"); }
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// The partial view '{0}' was not found. The following locations were searched:{1}
|
||||
/// </summary>
|
||||
internal static string FormatViewEngine_PartialViewNotFound(object p0, object p1)
|
||||
{
|
||||
return string.Format(CultureInfo.CurrentCulture, GetString("ViewEngine_PartialViewNotFound"), p0, p1);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// The view '{0}' was not found. The following locations were searched:{1}.
|
||||
/// </summary>
|
||||
internal static string ViewEngine_ViewNotFound
|
||||
{
|
||||
get { return GetString("ViewEngine_ViewNotFound"); }
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// The view '{0}' was not found. The following locations were searched:{1}.
|
||||
/// </summary>
|
||||
internal static string FormatViewEngine_ViewNotFound(object p0, object p1)
|
||||
{
|
||||
return string.Format(CultureInfo.CurrentCulture, GetString("ViewEngine_ViewNotFound"), p0, p1);
|
||||
}
|
||||
|
||||
private static string GetString(string name, params string[] formatterNames)
|
||||
{
|
||||
var value = _resourceManager.GetString(name);
|
||||
|
|
|
|||
|
|
@ -126,4 +126,10 @@
|
|||
<data name="RenderBodyCannotBeCalled" xml:space="preserve">
|
||||
<value>RenderBody can only be called from a layout page.</value>
|
||||
</data>
|
||||
<data name="ViewEngine_PartialViewNotFound" xml:space="preserve">
|
||||
<value>The partial view '{0}' was not found. The following locations were searched:{1}</value>
|
||||
</data>
|
||||
<data name="ViewEngine_ViewNotFound" xml:space="preserve">
|
||||
<value>The view '{0}' was not found. The following locations were searched:{1}.</value>
|
||||
</data>
|
||||
</root>
|
||||
|
|
@ -28,7 +28,34 @@ namespace Microsoft.AspNet.Mvc.Razor
|
|||
get { return _viewLocationFormats; }
|
||||
}
|
||||
|
||||
public async Task<ViewEngineResult> FindView([NotNull] IDictionary<string, object> context,[NotNull] string viewName)
|
||||
public async Task<ViewEngineResult> FindView([NotNull] IDictionary<string, object> context,
|
||||
[NotNull] string viewName)
|
||||
{
|
||||
var viewEngineResult = await CreateViewEngineResult(context, viewName);
|
||||
var errorMessage = Resources.FormatViewEngine_ViewNotFound(
|
||||
viewName,
|
||||
ToLocationString(viewEngineResult.SearchedLocations));
|
||||
|
||||
EnsureViewEngineResult(viewEngineResult, viewName, errorMessage);
|
||||
|
||||
return viewEngineResult;
|
||||
}
|
||||
|
||||
public async Task<ViewEngineResult> FindPartialView([NotNull] IDictionary<string, object> context,
|
||||
[NotNull] string partialViewName)
|
||||
{
|
||||
var viewEngineResult = await CreateViewEngineResult(context, partialViewName);
|
||||
var errorMessage = Resources.FormatViewEngine_PartialViewNotFound(
|
||||
partialViewName,
|
||||
ToLocationString(viewEngineResult.SearchedLocations));
|
||||
|
||||
EnsureViewEngineResult(viewEngineResult, partialViewName, errorMessage);
|
||||
|
||||
return viewEngineResult;
|
||||
}
|
||||
|
||||
private async Task<ViewEngineResult> CreateViewEngineResult([NotNull] IDictionary<string, object> context,
|
||||
[NotNull] string viewName)
|
||||
{
|
||||
var nameRepresentsPath = IsSpecificPath(viewName);
|
||||
|
||||
|
|
@ -59,6 +86,24 @@ namespace Microsoft.AspNet.Mvc.Razor
|
|||
}
|
||||
}
|
||||
|
||||
private void EnsureViewEngineResult(ViewEngineResult viewEngineResult, string viewName, string errorMessage)
|
||||
{
|
||||
if (!viewEngineResult.Success)
|
||||
{
|
||||
throw new InvalidOperationException(errorMessage);
|
||||
}
|
||||
}
|
||||
|
||||
private string ToLocationString(IEnumerable<string> locations)
|
||||
{
|
||||
if (locations != null)
|
||||
{
|
||||
return Environment.NewLine + string.Join(Environment.NewLine, locations);
|
||||
}
|
||||
|
||||
return string.Empty;
|
||||
}
|
||||
|
||||
private static bool IsSpecificPath(string name)
|
||||
{
|
||||
char c = name[0];
|
||||
|
|
|
|||
|
|
@ -141,7 +141,4 @@
|
|||
<data name="ViewData_WrongTModelType" xml:space="preserve">
|
||||
<value>The model item passed into the ViewDataDictionary is of type '{0}', but this ViewDataDictionary instance requires a model item of type '{1}'.</value>
|
||||
</data>
|
||||
<data name="ViewEngine_ViewNotFound" xml:space="preserve">
|
||||
<value>The view '{0}' was not found. The following locations were searched:{1}.</value>
|
||||
</data>
|
||||
</root>
|
||||
|
|
@ -6,5 +6,6 @@ namespace Microsoft.AspNet.Mvc.Rendering
|
|||
public interface IViewEngine
|
||||
{
|
||||
Task<ViewEngineResult> FindView([NotNull] IDictionary<string, object> context, [NotNull] string viewName);
|
||||
Task<ViewEngineResult> FindPartialView([NotNull] IDictionary<string, object> context, [NotNull] string partialViewName);
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -20,7 +20,8 @@ namespace Microsoft.AspNet.Mvc.Rendering
|
|||
get { return View != null; }
|
||||
}
|
||||
|
||||
public static ViewEngineResult NotFound([NotNull] string viewName, [NotNull] IEnumerable<string> searchedLocations)
|
||||
public static ViewEngineResult NotFound([NotNull] string viewName,
|
||||
[NotNull] IEnumerable<string> searchedLocations)
|
||||
{
|
||||
return new ViewEngineResult
|
||||
{
|
||||
|
|
@ -37,18 +38,5 @@ namespace Microsoft.AspNet.Mvc.Rendering
|
|||
ViewName = viewName,
|
||||
};
|
||||
}
|
||||
|
||||
public ViewEngineResult EnsureSuccess()
|
||||
{
|
||||
if (Success)
|
||||
{
|
||||
return this;
|
||||
}
|
||||
|
||||
var locationsText = Environment.NewLine + string.Join(Environment.NewLine, SearchedLocations);
|
||||
throw new InvalidOperationException(Resources.FormatViewEngine_ViewNotFound(
|
||||
ViewName,
|
||||
locationsText));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in New Issue