Skip to content
This repository has been archived by the owner on Dec 14, 2018. It is now read-only.

Commonize 'throw on view not found' #140

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 1 addition & 8 deletions src/Microsoft.AspNet.Mvc.Core/ActionResults/ViewResult.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
using System;
using System.Collections.Generic;
using System.Globalization;
using System.IO;
using System.Text;
using System.Threading.Tasks;
Expand Down Expand Up @@ -43,13 +42,7 @@ public async Task ExecuteResultAsync([NotNull] ActionContext context)
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.ThrowIfViewNotFound();
return result.View;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -76,19 +76,8 @@ public async Task ExecuteAsync([NotNull] ViewComponentContext context)

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.ThrowIfViewNotFound();
return result.View;
}
}
Expand Down
13 changes: 4 additions & 9 deletions src/Microsoft.AspNet.Mvc.Razor/ViewEngine/RazorViewEngine.cs
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,8 @@ public async Task<ViewEngineResult> FindView([NotNull] IDictionary<string, objec
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
{
Expand All @@ -50,20 +50,15 @@ public async Task<ViewEngineResult> FindView([NotNull] IDictionary<string, objec
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];
Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions src/Microsoft.AspNet.Mvc.Rendering/Properties/Resources.resx
Original file line number Diff line number Diff line change
Expand Up @@ -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>
25 changes: 21 additions & 4 deletions src/Microsoft.AspNet.Mvc.Rendering/View/ViewEngineResult.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,25 +13,42 @@ private ViewEngineResult()

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 void ThrowIfViewNotFound()
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

public ViewEngineResult EnsureSuccessful().

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

{
if (Success)
{
return;
}

var locationsText = Environment.NewLine + string.Join(Environment.NewLine, SearchedLocations);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@Tratcher it would be nice if the error page preserved formatting (pre tag). This way we don't need to do anything special for printing these out.

Bonus points if you get it to support markdown

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

File a bug in DiagnosticPages.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

AspNet/DiagnosticPages#6 logged by @pranavkm thanks!

throw new InvalidOperationException(Resources.FormatViewEngine_ViewNotFound(
ViewName,
locationsText));
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Extra space

}
}
}