Refactor FindView to always take a viewName (no null support)

and only use RouteValues (or generically just Dictionary<string, object>).

This is temporary and will change once we get Partials (which are currently just dead code)

For now this will unblock ViewComponents
This commit is contained in:
Yishai Galatzer 2014-03-19 18:47:43 -07:00
parent f8dd52dfe3
commit dcc286a299
5 changed files with 15 additions and 56 deletions

View File

@ -1,4 +1,5 @@
using System;
using System.Collections.Generic;
using System.Globalization;
using System.IO;
using System.Text;
@ -22,14 +23,11 @@ namespace Microsoft.AspNet.Mvc
public ViewData ViewData { get; set; }
public async Task ExecuteResultAsync(ActionContext context)
public async Task ExecuteResultAsync([NotNull] ActionContext context)
{
if (context == null)
{
throw new ArgumentNullException("context");
}
var viewName = ViewName ?? context.ActionDescriptor.Name;
var view = await FindView(context.RouteValues, viewName);
IView view = await FindView(context, ViewName);
using (view as IDisposable)
{
context.HttpContext.Response.ContentType = "text/html";
@ -44,12 +42,12 @@ namespace Microsoft.AspNet.Mvc
}
}
private async Task<IView> FindView(ActionContext actionContext, string viewName)
private async Task<IView> FindView([NotNull] IDictionary<string, object> context,[NotNull] string viewName)
{
ViewEngineResult result = await _viewEngine.FindView(actionContext, viewName);
var result = await _viewEngine.FindView(context, viewName);
if (!result.Success)
{
string locationsText = string.Join(Environment.NewLine, result.SearchedLocations);
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));
}

View File

@ -31,8 +31,7 @@ namespace Microsoft.AspNet.Mvc
public IActionResult View(string view)
{
object model = null;
return View(view, model);
return View(view, model: (object)null);
}
public IActionResult View<TModel>(TModel model)

View File

@ -28,27 +28,8 @@ namespace Microsoft.AspNet.Mvc.Razor
get { return _viewLocationFormats; }
}
public async Task<ViewEngineResult> FindView(object context, string viewName)
public async Task<ViewEngineResult> FindView([NotNull] IDictionary<string, object> context,[NotNull] string viewName)
{
var actionContext = (ActionContext)context;
var actionDescriptor = actionContext.ActionDescriptor;
if (actionDescriptor == null)
{
return null;
}
if (string.IsNullOrEmpty(viewName))
{
viewName = actionDescriptor.Name;
}
if (string.IsNullOrEmpty(viewName))
{
throw new ArgumentException(Resources.ArgumentCannotBeNullOrEmpty, "viewName");
}
var nameRepresentsPath = IsSpecificPath(viewName);
if (nameRepresentsPath)
@ -59,8 +40,8 @@ namespace Microsoft.AspNet.Mvc.Razor
}
else
{
var controllerName = actionContext.RouteValues.GetValueOrDefault<string>("controller");
var areaName = actionContext.RouteValues.GetValueOrDefault<string>("area");
var controllerName = context.GetValueOrDefault<string>("controller");
var areaName = context.GetValueOrDefault<string>("area");
var searchedLocations = new List<string>(_viewLocationFormats.Length);
for (int i = 0; i < _viewLocationFormats.Length; i++)
@ -73,6 +54,7 @@ namespace Microsoft.AspNet.Mvc.Razor
}
searchedLocations.Add(path);
}
return ViewEngineResult.NotFound(searchedLocations);
}
}

View File

@ -83,26 +83,6 @@ namespace Microsoft.AspNet.Mvc.Rendering
return value != null ? WebUtility.HtmlEncode(value.ToString()) : string.Empty;
}
internal static IView FindPartialView([NotNull] ViewContext viewContext, string partialViewName,
[NotNull] IViewEngine viewEngine)
{
ViewEngineResult result = viewEngine.FindView(viewContext, partialViewName).Result;
if (result.View != null)
{
return result.View;
}
StringBuilder locationsText = new StringBuilder();
foreach (string location in result.SearchedLocations)
{
locationsText.AppendLine();
locationsText.Append(location);
}
throw new InvalidOperationException(Resources.FormatCommon_PartialViewNotFound(partialViewName,
locationsText));
}
public string GenerateIdFromName([NotNull] string name)
{
return TagBuilder.CreateSanitizedId(name, IdAttributeDotReplacement);

View File

@ -1,10 +1,10 @@
using System.Threading.Tasks;
using System.Collections.Generic;
using System.Threading.Tasks;
namespace Microsoft.AspNet.Mvc.Rendering
{
public interface IViewEngine
{
// TODO: Relayer to allow this to be ActionContext. We probably need the common MVC assembly
Task<ViewEngineResult> FindView(object actionContext, string viewName);
Task<ViewEngineResult> FindView([NotNull] IDictionary<string, object> context, [NotNull] string viewName);
}
}