From a5ed1157d514004b60834b80faaf3d39a93f6a7e Mon Sep 17 00:00:00 2001 From: "N. Taylor Mullen" Date: Thu, 27 Mar 2014 17:06:34 -0700 Subject: [PATCH] Refactor ViewEngine to support partials. This references WEBFX-95 and is the preliminary part to support Partial helpers. --- .../ActionResults/ViewResult.cs | 2 +- .../ViewComponents/ViewViewComponentResult.cs | 2 +- .../Properties/Resources.Designer.cs | 32 +++++++++++++ src/Microsoft.AspNet.Mvc.Razor/Resources.resx | 6 +++ .../ViewEngine/RazorViewEngine.cs | 47 ++++++++++++++++++- .../Properties/Resources.resx | 3 -- .../View/IViewEngine.cs | 1 + .../View/ViewEngineResult.cs | 16 +------ 8 files changed, 89 insertions(+), 20 deletions(-) diff --git a/src/Microsoft.AspNet.Mvc.Core/ActionResults/ViewResult.cs b/src/Microsoft.AspNet.Mvc.Core/ActionResults/ViewResult.cs index 95516bf0ae..fc8c70c364 100644 --- a/src/Microsoft.AspNet.Mvc.Core/ActionResults/ViewResult.cs +++ b/src/Microsoft.AspNet.Mvc.Core/ActionResults/ViewResult.cs @@ -42,7 +42,7 @@ namespace Microsoft.AspNet.Mvc private async Task FindView([NotNull] IDictionary context,[NotNull] string viewName) { var result = await _viewEngine.FindView(context, viewName); - result.EnsureSuccess(); + return result.View; } diff --git a/src/Microsoft.AspNet.Mvc.Core/ViewComponents/ViewViewComponentResult.cs b/src/Microsoft.AspNet.Mvc.Core/ViewComponents/ViewViewComponentResult.cs index fb0f8b2542..ac60243972 100644 --- a/src/Microsoft.AspNet.Mvc.Core/ViewComponents/ViewViewComponentResult.cs +++ b/src/Microsoft.AspNet.Mvc.Core/ViewComponents/ViewViewComponentResult.cs @@ -78,7 +78,7 @@ namespace Microsoft.AspNet.Mvc private async Task FindView([NotNull] IDictionary context, [NotNull] string viewName) { var result = await _viewEngine.FindView(context, viewName); - result.EnsureSuccess(); + return result.View; } } diff --git a/src/Microsoft.AspNet.Mvc.Razor/Properties/Resources.Designer.cs b/src/Microsoft.AspNet.Mvc.Razor/Properties/Resources.Designer.cs index daa99ccf2a..ed41ce5d4e 100644 --- a/src/Microsoft.AspNet.Mvc.Razor/Properties/Resources.Designer.cs +++ b/src/Microsoft.AspNet.Mvc.Razor/Properties/Resources.Designer.cs @@ -58,6 +58,38 @@ namespace Microsoft.AspNet.Mvc.Razor return GetString("RenderBodyCannotBeCalled"); } + /// + /// The partial view '{0}' was not found. The following locations were searched:{1} + /// + internal static string ViewEngine_PartialViewNotFound + { + get { return GetString("ViewEngine_PartialViewNotFound"); } + } + + /// + /// The partial view '{0}' was not found. The following locations were searched:{1} + /// + internal static string FormatViewEngine_PartialViewNotFound(object p0, object p1) + { + return string.Format(CultureInfo.CurrentCulture, GetString("ViewEngine_PartialViewNotFound"), p0, p1); + } + + /// + /// The view '{0}' was not found. The following locations were searched:{1}. + /// + internal static string ViewEngine_ViewNotFound + { + get { return GetString("ViewEngine_ViewNotFound"); } + } + + /// + /// The view '{0}' was not found. The following locations were searched:{1}. + /// + 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); diff --git a/src/Microsoft.AspNet.Mvc.Razor/Resources.resx b/src/Microsoft.AspNet.Mvc.Razor/Resources.resx index 15174f1865..54573601cc 100644 --- a/src/Microsoft.AspNet.Mvc.Razor/Resources.resx +++ b/src/Microsoft.AspNet.Mvc.Razor/Resources.resx @@ -126,4 +126,10 @@ RenderBody can only be called from a layout page. + + The partial view '{0}' was not found. The following locations were searched:{1} + + + The view '{0}' was not found. The following locations were searched:{1}. + \ No newline at end of file diff --git a/src/Microsoft.AspNet.Mvc.Razor/ViewEngine/RazorViewEngine.cs b/src/Microsoft.AspNet.Mvc.Razor/ViewEngine/RazorViewEngine.cs index 3d15c05d5d..f01d212dad 100644 --- a/src/Microsoft.AspNet.Mvc.Razor/ViewEngine/RazorViewEngine.cs +++ b/src/Microsoft.AspNet.Mvc.Razor/ViewEngine/RazorViewEngine.cs @@ -28,7 +28,34 @@ namespace Microsoft.AspNet.Mvc.Razor get { return _viewLocationFormats; } } - public async Task FindView([NotNull] IDictionary context,[NotNull] string viewName) + public async Task FindView([NotNull] IDictionary 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 FindPartialView([NotNull] IDictionary 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 CreateViewEngineResult([NotNull] IDictionary 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 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]; diff --git a/src/Microsoft.AspNet.Mvc.Rendering/Properties/Resources.resx b/src/Microsoft.AspNet.Mvc.Rendering/Properties/Resources.resx index d4f1444d9a..deb8e5ac34 100644 --- a/src/Microsoft.AspNet.Mvc.Rendering/Properties/Resources.resx +++ b/src/Microsoft.AspNet.Mvc.Rendering/Properties/Resources.resx @@ -141,7 +141,4 @@ The model item passed into the ViewDataDictionary is of type '{0}', but this ViewDataDictionary instance requires a model item of type '{1}'. - - The view '{0}' was not found. The following locations were searched:{1}. - \ No newline at end of file diff --git a/src/Microsoft.AspNet.Mvc.Rendering/View/IViewEngine.cs b/src/Microsoft.AspNet.Mvc.Rendering/View/IViewEngine.cs index 8b69c0a7c3..b1325af6b4 100644 --- a/src/Microsoft.AspNet.Mvc.Rendering/View/IViewEngine.cs +++ b/src/Microsoft.AspNet.Mvc.Rendering/View/IViewEngine.cs @@ -6,5 +6,6 @@ namespace Microsoft.AspNet.Mvc.Rendering public interface IViewEngine { Task FindView([NotNull] IDictionary context, [NotNull] string viewName); + Task FindPartialView([NotNull] IDictionary context, [NotNull] string partialViewName); } } diff --git a/src/Microsoft.AspNet.Mvc.Rendering/View/ViewEngineResult.cs b/src/Microsoft.AspNet.Mvc.Rendering/View/ViewEngineResult.cs index 207e0365ee..a8316bd513 100644 --- a/src/Microsoft.AspNet.Mvc.Rendering/View/ViewEngineResult.cs +++ b/src/Microsoft.AspNet.Mvc.Rendering/View/ViewEngineResult.cs @@ -20,7 +20,8 @@ namespace Microsoft.AspNet.Mvc.Rendering get { return View != null; } } - public static ViewEngineResult NotFound([NotNull] string viewName, [NotNull] IEnumerable searchedLocations) + public static ViewEngineResult NotFound([NotNull] string viewName, + [NotNull] IEnumerable 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)); - } } }