Commonize 'throw on view not found'

Deduping this code by moving it to ViewEngineResult.
This commit is contained in:
Ryan Nowak 2014-03-25 12:22:21 -07:00
parent fa21d1cd65
commit 6fb0d5b282
6 changed files with 46 additions and 33 deletions

View File

@ -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<IView> FindView([NotNull] IDictionary<string, object> 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 &apos;{0}&apos; was not found. The following locations were searched:{1}.";
throw new InvalidOperationException(String.Format(CultureInfo.CurrentCulture, message, viewName, locationsText));
}
result.EnsureSuccess();
return result.View;
}

View File

@ -76,19 +76,8 @@ namespace Microsoft.AspNet.Mvc
private async Task<IView> FindView([NotNull] IDictionary<string, object> 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 &apos;{0}&apos; was not found. The following locations were searched:{1}.";
throw new InvalidOperationException(String.Format(
CultureInfo.CurrentCulture,
message,
viewName,
locationsText));
}
result.EnsureSuccess();
return result.View;
}
}

View File

@ -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<ViewEngineResult> FindComponentView(object actionContext, string viewName)
{
throw new NotImplementedException();
}
private static bool IsSpecificPath(string name)
{
char c = name[0];

View File

@ -74,6 +74,22 @@ namespace Microsoft.AspNet.Mvc.Rendering
return string.Format(CultureInfo.CurrentCulture, GetString("ViewData_WrongTModelType"), 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);

View File

@ -129,4 +129,7 @@
<data name="ViewData_WrongTModelType" xml:space="preserve">
<value>The model item passed into the ViewData is of type '{0}', but this ViewData 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>

View File

@ -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<string> searchedLocations)
public static ViewEngineResult NotFound([NotNull] string viewName, [NotNull] IEnumerable<string> 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));
}
}
}