From 6fb0d5b2822a0a0dcef71ad432334931e13f713d Mon Sep 17 00:00:00 2001 From: Ryan Nowak Date: Tue, 25 Mar 2014 12:22:21 -0700 Subject: [PATCH] Commonize 'throw on view not found' Deduping this code by moving it to ViewEngineResult. --- .../ActionResults/ViewResult.cs | 9 +------ .../ViewComponents/ViewViewComponentResult.cs | 13 +--------- .../ViewEngine/RazorViewEngine.cs | 13 +++------- .../Properties/Resources.Designer.cs | 16 ++++++++++++ .../Properties/Resources.resx | 3 +++ .../View/ViewEngineResult.cs | 25 ++++++++++++++++--- 6 files changed, 46 insertions(+), 33 deletions(-) diff --git a/src/Microsoft.AspNet.Mvc.Core/ActionResults/ViewResult.cs b/src/Microsoft.AspNet.Mvc.Core/ActionResults/ViewResult.cs index 2a43599255..d1629af2cc 100644 --- a/src/Microsoft.AspNet.Mvc.Core/ActionResults/ViewResult.cs +++ b/src/Microsoft.AspNet.Mvc.Core/ActionResults/ViewResult.cs @@ -1,6 +1,5 @@ using System; using System.Collections.Generic; -using System.Globalization; using System.IO; using System.Text; using System.Threading.Tasks; @@ -43,13 +42,7 @@ namespace Microsoft.AspNet.Mvc private async Task FindView([NotNull] IDictionary context,[NotNull] string viewName) { var result = await _viewEngine.FindView(context, viewName); - if (!result.Success) - { - var locationsText = string.Join(Environment.NewLine, result.SearchedLocations); - const string message = @"The view '{0}' was not found. The following locations were searched:{1}."; - throw new InvalidOperationException(String.Format(CultureInfo.CurrentCulture, message, viewName, locationsText)); - } - + 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 3d70ced0a1..30c4391ea1 100644 --- a/src/Microsoft.AspNet.Mvc.Core/ViewComponents/ViewViewComponentResult.cs +++ b/src/Microsoft.AspNet.Mvc.Core/ViewComponents/ViewViewComponentResult.cs @@ -76,19 +76,8 @@ namespace Microsoft.AspNet.Mvc private async Task FindView([NotNull] IDictionary context, [NotNull] string viewName) { - // Issue #161 in Jira tracks unduping this code. var result = await _viewEngine.FindView(context, viewName); - if (!result.Success) - { - var locationsText = string.Join(Environment.NewLine, result.SearchedLocations); - const string message = @"The view '{0}' was not found. The following locations were searched:{1}."; - throw new InvalidOperationException(String.Format( - CultureInfo.CurrentCulture, - message, - viewName, - locationsText)); - } - + result.EnsureSuccess(); return result.View; } } diff --git a/src/Microsoft.AspNet.Mvc.Razor/ViewEngine/RazorViewEngine.cs b/src/Microsoft.AspNet.Mvc.Razor/ViewEngine/RazorViewEngine.cs index c3c7a88b4e..3d15c05d5d 100644 --- a/src/Microsoft.AspNet.Mvc.Razor/ViewEngine/RazorViewEngine.cs +++ b/src/Microsoft.AspNet.Mvc.Razor/ViewEngine/RazorViewEngine.cs @@ -35,8 +35,8 @@ namespace Microsoft.AspNet.Mvc.Razor if (nameRepresentsPath) { var view = await _virtualPathFactory.CreateInstance(viewName); - return view != null ? ViewEngineResult.Found(view) : - ViewEngineResult.NotFound(new[] { viewName }); + return view != null ? ViewEngineResult.Found(viewName, view) : + ViewEngineResult.NotFound(viewName, new[] { viewName }); } else { @@ -50,20 +50,15 @@ namespace Microsoft.AspNet.Mvc.Razor IView view = await _virtualPathFactory.CreateInstance(path); if (view != null) { - return ViewEngineResult.Found(view); + return ViewEngineResult.Found(viewName, view); } searchedLocations.Add(path); } - return ViewEngineResult.NotFound(searchedLocations); + return ViewEngineResult.NotFound(viewName, searchedLocations); } } - public Task FindComponentView(object actionContext, string viewName) - { - throw new NotImplementedException(); - } - private static bool IsSpecificPath(string name) { char c = name[0]; diff --git a/src/Microsoft.AspNet.Mvc.Rendering/Properties/Resources.Designer.cs b/src/Microsoft.AspNet.Mvc.Rendering/Properties/Resources.Designer.cs index 355f42fc86..48fade35b3 100644 --- a/src/Microsoft.AspNet.Mvc.Rendering/Properties/Resources.Designer.cs +++ b/src/Microsoft.AspNet.Mvc.Rendering/Properties/Resources.Designer.cs @@ -74,6 +74,22 @@ namespace Microsoft.AspNet.Mvc.Rendering return string.Format(CultureInfo.CurrentCulture, GetString("ViewData_WrongTModelType"), 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.Rendering/Properties/Resources.resx b/src/Microsoft.AspNet.Mvc.Rendering/Properties/Resources.resx index 6d42ab2a58..b47862e0d4 100644 --- a/src/Microsoft.AspNet.Mvc.Rendering/Properties/Resources.resx +++ b/src/Microsoft.AspNet.Mvc.Rendering/Properties/Resources.resx @@ -129,4 +129,7 @@ The model item passed into the ViewData is of type '{0}', but this ViewData 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/ViewEngineResult.cs b/src/Microsoft.AspNet.Mvc.Rendering/View/ViewEngineResult.cs index 3ef3e86527..207e0365ee 100644 --- a/src/Microsoft.AspNet.Mvc.Rendering/View/ViewEngineResult.cs +++ b/src/Microsoft.AspNet.Mvc.Rendering/View/ViewEngineResult.cs @@ -13,25 +13,42 @@ namespace Microsoft.AspNet.Mvc.Rendering public IView View { get; private set; } + public string ViewName { get; private set; } + public bool Success { get { return View != null; } } - public static ViewEngineResult NotFound([NotNull] IEnumerable searchedLocations) + public static ViewEngineResult NotFound([NotNull] string viewName, [NotNull] IEnumerable searchedLocations) { return new ViewEngineResult { - SearchedLocations = searchedLocations + SearchedLocations = searchedLocations, + ViewName = viewName, }; } - public static ViewEngineResult Found([NotNull] IView view) + public static ViewEngineResult Found([NotNull] string viewName, [NotNull] IView view) { return new ViewEngineResult { - View = view + View = view, + 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)); + } } }