From dcc286a299edf6c1d906ee8b63a0503d1e471b2b Mon Sep 17 00:00:00 2001 From: Yishai Galatzer Date: Wed, 19 Mar 2014 18:47:43 -0700 Subject: [PATCH] Refactor FindView to always take a viewName (no null support) and only use RouteValues (or generically just Dictionary). This is temporary and will change once we get Partials (which are currently just dead code) For now this will unblock ViewComponents --- .../ActionResults/ViewResult.cs | 16 +++++------- src/Microsoft.AspNet.Mvc.Core/Controller.cs | 3 +-- .../ViewEngine/RazorViewEngine.cs | 26 +++---------------- .../Html/HtmlHelper.cs | 20 -------------- .../View/IViewEngine.cs | 6 ++--- 5 files changed, 15 insertions(+), 56 deletions(-) diff --git a/src/Microsoft.AspNet.Mvc.Core/ActionResults/ViewResult.cs b/src/Microsoft.AspNet.Mvc.Core/ActionResults/ViewResult.cs index c473b3b77e..729ed5c227 100644 --- a/src/Microsoft.AspNet.Mvc.Core/ActionResults/ViewResult.cs +++ b/src/Microsoft.AspNet.Mvc.Core/ActionResults/ViewResult.cs @@ -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 FindView(ActionContext actionContext, string viewName) + private async Task FindView([NotNull] IDictionary 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 '{0}' was not found. The following locations were searched:{1}."; throw new InvalidOperationException(String.Format(CultureInfo.CurrentCulture, message, viewName, locationsText)); } diff --git a/src/Microsoft.AspNet.Mvc.Core/Controller.cs b/src/Microsoft.AspNet.Mvc.Core/Controller.cs index a1d1a0936d..4f0a870f72 100644 --- a/src/Microsoft.AspNet.Mvc.Core/Controller.cs +++ b/src/Microsoft.AspNet.Mvc.Core/Controller.cs @@ -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 model) diff --git a/src/Microsoft.AspNet.Mvc.Razor/ViewEngine/RazorViewEngine.cs b/src/Microsoft.AspNet.Mvc.Razor/ViewEngine/RazorViewEngine.cs index ea19ea43ae..a70e9103eb 100644 --- a/src/Microsoft.AspNet.Mvc.Razor/ViewEngine/RazorViewEngine.cs +++ b/src/Microsoft.AspNet.Mvc.Razor/ViewEngine/RazorViewEngine.cs @@ -28,27 +28,8 @@ namespace Microsoft.AspNet.Mvc.Razor get { return _viewLocationFormats; } } - public async Task FindView(object context, string viewName) + public async Task FindView([NotNull] IDictionary 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("controller"); - var areaName = actionContext.RouteValues.GetValueOrDefault("area"); + var controllerName = context.GetValueOrDefault("controller"); + var areaName = context.GetValueOrDefault("area"); var searchedLocations = new List(_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); } } diff --git a/src/Microsoft.AspNet.Mvc.Rendering/Html/HtmlHelper.cs b/src/Microsoft.AspNet.Mvc.Rendering/Html/HtmlHelper.cs index 1836c47795..99ba0701f3 100644 --- a/src/Microsoft.AspNet.Mvc.Rendering/Html/HtmlHelper.cs +++ b/src/Microsoft.AspNet.Mvc.Rendering/Html/HtmlHelper.cs @@ -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); diff --git a/src/Microsoft.AspNet.Mvc.Rendering/View/IViewEngine.cs b/src/Microsoft.AspNet.Mvc.Rendering/View/IViewEngine.cs index 4223b3573c..8b69c0a7c3 100644 --- a/src/Microsoft.AspNet.Mvc.Rendering/View/IViewEngine.cs +++ b/src/Microsoft.AspNet.Mvc.Rendering/View/IViewEngine.cs @@ -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 FindView(object actionContext, string viewName); + Task FindView([NotNull] IDictionary context, [NotNull] string viewName); } }