diff --git a/src/Microsoft.AspNet.Mvc.Core/ActionResults/ViewResult.cs b/src/Microsoft.AspNet.Mvc.Core/ActionResults/ViewResult.cs index e4acdb1da0..08e8f933cb 100644 --- a/src/Microsoft.AspNet.Mvc.Core/ActionResults/ViewResult.cs +++ b/src/Microsoft.AspNet.Mvc.Core/ActionResults/ViewResult.cs @@ -4,6 +4,7 @@ using System.IO; using System.Text; using System.Threading.Tasks; using Microsoft.AspNet.DependencyInjection; +using Microsoft.AspNet.Mvc.Core; using Microsoft.AspNet.Mvc.Rendering; namespace Microsoft.AspNet.Mvc @@ -42,6 +43,18 @@ namespace Microsoft.AspNet.Mvc private IView FindView([NotNull] IDictionary context, [NotNull] string viewName) { var result = _viewEngine.FindView(context, viewName); + if (!result.Success) + { + var locations = string.Empty; + if (result.SearchedLocations != null) + { + locations = Environment.NewLine + + string.Join(Environment.NewLine, result.SearchedLocations); + } + + throw new InvalidOperationException(Resources.FormatViewEngine_ViewNotFound(viewName, locations)); + } + return result.View; } } diff --git a/src/Microsoft.AspNet.Mvc.Core/Properties/Resources.Designer.cs b/src/Microsoft.AspNet.Mvc.Core/Properties/Resources.Designer.cs index 4ec1b53b2d..7674c4ac67 100644 --- a/src/Microsoft.AspNet.Mvc.Core/Properties/Resources.Designer.cs +++ b/src/Microsoft.AspNet.Mvc.Core/Properties/Resources.Designer.cs @@ -586,6 +586,38 @@ namespace Microsoft.AspNet.Mvc.Core return string.Format(CultureInfo.CurrentCulture, GetString("AsyncResultFilter_InvalidShortCircuit"), p0, p1, p2, p3); } + /// + /// 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.Core/Rendering/Html/HtmlHelper.cs b/src/Microsoft.AspNet.Mvc.Core/Rendering/Html/HtmlHelper.cs index b394853184..b8646ad9b6 100644 --- a/src/Microsoft.AspNet.Mvc.Core/Rendering/Html/HtmlHelper.cs +++ b/src/Microsoft.AspNet.Mvc.Core/Rendering/Html/HtmlHelper.cs @@ -321,11 +321,21 @@ namespace Microsoft.AspNet.Mvc.Rendering var newViewData = new ViewDataDictionary(baseViewData, model); - - var viewEngineResult = _viewEngine.FindPartialView(ViewContext.RouteValues, partialViewName); - var view = viewEngineResult.View; + if (!viewEngineResult.Success) + { + var locations = string.Empty; + if (viewEngineResult.SearchedLocations != null) + { + locations = Environment.NewLine + + string.Join(Environment.NewLine, viewEngineResult.SearchedLocations); + } + throw new InvalidOperationException( + Resources.FormatViewEngine_PartialViewNotFound(partialViewName, locations)); + } + + var view = viewEngineResult.View; using (view as IDisposable) { var viewContext = new ViewContext(ViewContext, view, newViewData, writer); diff --git a/src/Microsoft.AspNet.Mvc.Core/Resources.resx b/src/Microsoft.AspNet.Mvc.Core/Resources.resx index e96f8c6796..791f8551f2 100644 --- a/src/Microsoft.AspNet.Mvc.Core/Resources.resx +++ b/src/Microsoft.AspNet.Mvc.Core/Resources.resx @@ -225,4 +225,10 @@ If an {0} cancels execution by setting the {1} property of {2} to 'true', then the it should not call the next filter by invoking {3}. + + 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.Core/ViewComponents/ViewViewComponentResult.cs b/src/Microsoft.AspNet.Mvc.Core/ViewComponents/ViewViewComponentResult.cs index 945c655dbf..65bcfd933e 100644 --- a/src/Microsoft.AspNet.Mvc.Core/ViewComponents/ViewViewComponentResult.cs +++ b/src/Microsoft.AspNet.Mvc.Core/ViewComponents/ViewViewComponentResult.cs @@ -3,6 +3,7 @@ using System; using System.Collections.Generic; using System.Globalization; using System.Threading.Tasks; +using Microsoft.AspNet.Mvc.Core; using Microsoft.AspNet.Mvc.Rendering; namespace Microsoft.AspNet.Mvc @@ -74,6 +75,18 @@ namespace Microsoft.AspNet.Mvc private IView FindView([NotNull] IDictionary context, [NotNull] string viewName) { var result = _viewEngine.FindView(context, viewName); + if (!result.Success) + { + var locations = string.Empty; + if (result.SearchedLocations != null) + { + locations = Environment.NewLine + + string.Join(Environment.NewLine, result.SearchedLocations); + } + + throw new InvalidOperationException(Resources.FormatViewEngine_ViewNotFound(viewName, locations)); + } + 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 7468917dbd..c8f7b67f8a 100644 --- a/src/Microsoft.AspNet.Mvc.Razor/Properties/Resources.Designer.cs +++ b/src/Microsoft.AspNet.Mvc.Razor/Properties/Resources.Designer.cs @@ -186,38 +186,6 @@ namespace Microsoft.AspNet.Mvc.Razor return string.Format(CultureInfo.CurrentCulture, GetString("SectionsNotRendered"), p0); } - /// - /// 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); - } - /// /// The method '{0}' cannot be invoked by this view. /// diff --git a/src/Microsoft.AspNet.Mvc.Razor/Resources.resx b/src/Microsoft.AspNet.Mvc.Razor/Resources.resx index 0fe0028e88..57bb7845be 100644 --- a/src/Microsoft.AspNet.Mvc.Razor/Resources.resx +++ b/src/Microsoft.AspNet.Mvc.Razor/Resources.resx @@ -150,12 +150,6 @@ The following sections have been defined but have not been rendered: '{0}'. - - 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}. - The method '{0}' cannot be invoked by this view. diff --git a/src/Microsoft.AspNet.Mvc.Razor/ViewEngine/RazorViewEngine.cs b/src/Microsoft.AspNet.Mvc.Razor/ViewEngine/RazorViewEngine.cs index 90a287bca2..4aa4d0c943 100644 --- a/src/Microsoft.AspNet.Mvc.Razor/ViewEngine/RazorViewEngine.cs +++ b/src/Microsoft.AspNet.Mvc.Razor/ViewEngine/RazorViewEngine.cs @@ -32,26 +32,13 @@ namespace Microsoft.AspNet.Mvc.Razor [NotNull] string viewName) { var viewEngineResult = CreateViewEngineResult(context, viewName); - var errorMessage = Resources.FormatViewEngine_ViewNotFound( - viewName, - ToLocationString(viewEngineResult.SearchedLocations)); - - EnsureViewEngineResult(viewEngineResult, viewName, errorMessage); - return viewEngineResult; } public ViewEngineResult FindPartialView([NotNull] IDictionary context, [NotNull] string partialViewName) { - var viewEngineResult = CreateViewEngineResult(context, partialViewName); - var errorMessage = Resources.FormatViewEngine_PartialViewNotFound( - partialViewName, - ToLocationString(viewEngineResult.SearchedLocations)); - - EnsureViewEngineResult(viewEngineResult, partialViewName, errorMessage); - - return viewEngineResult; + return FindView(context, partialViewName); } private ViewEngineResult CreateViewEngineResult([NotNull] IDictionary context, @@ -86,24 +73,6 @@ 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];