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

Commit

Permalink
Modify FindView and FindPartialView to accept ActionContext
Browse files Browse the repository at this point in the history
Fixes #787
  • Loading branch information
pranavkm committed Jul 15, 2014
1 parent f6106e4 commit 5778a4b
Show file tree
Hide file tree
Showing 16 changed files with 150 additions and 98 deletions.
7 changes: 2 additions & 5 deletions src/Microsoft.AspNet.Mvc.Core/ActionResults/ViewResult.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ public override async Task ExecuteResultAsync([NotNull] ActionContext context)
var viewEngine = ViewEngine ?? context.HttpContext.RequestServices.GetService<ICompositeViewEngine>();

var viewName = ViewName ?? context.ActionDescriptor.Name;
var view = FindView(viewEngine, context.RouteData.Values, viewName);
var view = FindView(viewEngine, context, viewName);

using (view as IDisposable)
{
Expand All @@ -52,10 +52,7 @@ public override async Task ExecuteResultAsync([NotNull] ActionContext context)
}
}

private static IView FindView(
[NotNull] IViewEngine viewEngine,
[NotNull] IDictionary<string, object> context,
[NotNull] string viewName)
private static IView FindView(IViewEngine viewEngine, ActionContext context, string viewName)
{
var result = viewEngine.FindView(context, viewName);
if (!result.Success)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
// Copyright (c) Microsoft Open Technologies, Inc. All rights reserved.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.

using System;
using System.Collections.Generic;
using System.Linq;

Expand All @@ -21,20 +20,20 @@ public CompositeViewEngine(IViewEngineProvider viewEngineProvider)
public IReadOnlyList<IViewEngine> ViewEngines { get; private set; }

/// <inheritdoc />
public ViewEngineResult FindPartialView([NotNull] IDictionary<string, object> context,
public ViewEngineResult FindPartialView([NotNull] ActionContext context,
[NotNull] string partialViewName)
{
return FindView(context, partialViewName, partial: true);
}

/// <inheritdoc />
public ViewEngineResult FindView([NotNull] IDictionary<string, object> context,
public ViewEngineResult FindView([NotNull] ActionContext context,
[NotNull] string viewName)
{
return FindView(context, viewName, partial: false);
}

private ViewEngineResult FindView(IDictionary<string, object> context,
private ViewEngineResult FindView(ActionContext context,
string viewName,
bool partial)
{
Expand All @@ -48,6 +47,7 @@ private ViewEngineResult FindView(IDictionary<string, object> context,
{
return result;
}

searchedLocations = searchedLocations.Concat(result.SearchedLocations);
}

Expand Down
2 changes: 1 addition & 1 deletion src/Microsoft.AspNet.Mvc.Core/Rendering/Html/HtmlHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -381,7 +381,7 @@ protected virtual async Task RenderPartialCoreAsync([NotNull] string partialView

var newViewData = new ViewDataDictionary(baseViewData, model);

var viewEngineResult = _viewEngine.FindPartialView(ViewContext.RouteData.Values, partialViewName);
var viewEngineResult = _viewEngine.FindPartialView(ViewContext, partialViewName);
if (!viewEngineResult.Success)
{
var locations = string.Empty;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ public string Render()
var fullViewName = modeViewPath + "/" + viewName;

// Forcing synchronous behavior so users don't have to await templates.
var viewEngineResult = _viewEngine.FindPartialView(_viewContext.RouteData.Values, fullViewName);
var viewEngineResult = _viewEngine.FindPartialView(_viewContext, fullViewName);
if (viewEngineResult.Success)
{
using (var writer = new StringWriter(CultureInfo.InvariantCulture))
Expand Down
23 changes: 17 additions & 6 deletions src/Microsoft.AspNet.Mvc.Core/Rendering/IViewEngine.cs
Original file line number Diff line number Diff line change
@@ -1,16 +1,27 @@
// Copyright (c) Microsoft Open Technologies, Inc. All rights reserved.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.

using System.Collections.Generic;

namespace Microsoft.AspNet.Mvc.Rendering
{
/// <summary>
/// Defines the contract for a view engine.
/// </summary>
public interface IViewEngine
{
ViewEngineResult FindView([NotNull] IDictionary<string, object> context, [NotNull] string viewName);
/// <summary>
/// Finds the specified view by using the specified action context.
/// </summary>
/// <param name="context">The action context.</param>
/// <param name="viewName">The name or full path to the view.</param>
/// <returns>A result representing the result of locating the view.</returns>
ViewEngineResult FindView(ActionContext context, string viewName);

ViewEngineResult FindPartialView(
[NotNull] IDictionary<string, object> context,
[NotNull] string partialViewName);
/// <summary>
/// Finds the specified partial view by using the specified action context.
/// </summary>
/// <param name="context">The action context.</param>
/// <param name="viewName">The name or full path to the view.</param>
/// <returns>A result representing the result of locating the view.</returns>
ViewEngineResult FindPartialView(ActionContext context, string partialViewName);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ public async Task ExecuteAsync([NotNull] ViewComponentContext context)
ViewName ?? "Default");
}

var view = FindView(context.ViewContext.RouteData.Values, qualifiedViewName);
var view = FindView(context.ViewContext, qualifiedViewName);

var childViewContext = new ViewContext(
context.ViewContext,
Expand All @@ -75,7 +75,7 @@ public async Task ExecuteAsync([NotNull] ViewComponentContext context)
}
}

private IView FindView([NotNull] IDictionary<string, object> context, [NotNull] string viewName)
private IView FindView(ActionContext context, string viewName)
{
var result = _viewEngine.FindView(context, viewName);
if (!result.Success)
Expand Down
26 changes: 20 additions & 6 deletions src/Microsoft.AspNet.Mvc.Razor/ViewEngine/RazorViewEngine.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@

namespace Microsoft.AspNet.Mvc.Razor
{
/// <summary>
/// Represents a view engine that is used to render a Web page that uses the Razor syntax.
/// </summary>
public class RazorViewEngine : IViewEngine
{
private const string ViewExtension = ".cshtml";
Expand All @@ -28,31 +31,41 @@ public class RazorViewEngine : IViewEngine

private readonly IVirtualPathViewFactory _virtualPathFactory;

/// <summary>
/// Initializes a new instance of the RazorViewEngine class.
/// </summary>
/// <param name="virtualPathFactory">The view factory used for instantiating Razor views.</param>
public RazorViewEngine(IVirtualPathViewFactory virtualPathFactory)
{
_virtualPathFactory = virtualPathFactory;
}

/// <summary>
/// Gets the formats used for view locations.
/// The mapping of format tokens to route values are as follows:
/// {0} - Controller Name, {1} - Action name, {2} - Area name.
/// </summary>
public IEnumerable<string> ViewLocationFormats
{
get { return _viewLocationFormats; }
}

public ViewEngineResult FindView([NotNull] IDictionary<string, object> context,
/// <inheritdoc />
public ViewEngineResult FindView([NotNull] ActionContext context,
[NotNull] string viewName)
{
var viewEngineResult = CreateViewEngineResult(context, viewName);
return viewEngineResult;
}

public ViewEngineResult FindPartialView([NotNull] IDictionary<string, object> context,
/// <inheritdoc />
public ViewEngineResult FindPartialView([NotNull] ActionContext context,
[NotNull] string partialViewName)
{
return FindView(context, partialViewName);
}

private ViewEngineResult CreateViewEngineResult([NotNull] IDictionary<string, object> context,
[NotNull] string viewName)
private ViewEngineResult CreateViewEngineResult(ActionContext context, string viewName)
{
var nameRepresentsPath = IsSpecificPath(viewName);

Expand All @@ -70,8 +83,9 @@ private ViewEngineResult CreateViewEngineResult([NotNull] IDictionary<string, ob
}
else
{
var controllerName = context.GetValueOrDefault<string>("controller");
var areaName = context.GetValueOrDefault<string>("area");
var routeValues = context.RouteData.Values;
var controllerName = routeValues.GetValueOrDefault<string>("controller");
var areaName = routeValues.GetValueOrDefault<string>("area");
var potentialPaths = GetViewSearchPaths(viewName, controllerName, areaName);

foreach (var path in potentialPaths)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,12 +64,12 @@ public void ViewEngineDescriptors_AddsTypesAndInstances()

private class TestViewEngine : IViewEngine
{
public ViewEngineResult FindPartialView([NotNull]IDictionary<string, object> context, [NotNull]string partialViewName)
public ViewEngineResult FindPartialView(ActionContext context, string partialViewName)
{
throw new NotImplementedException();
}

public ViewEngineResult FindView([NotNull]IDictionary<string, object> context, [NotNull]string viewName)
public ViewEngineResult FindView(ActionContext context, string viewName)
{
throw new NotImplementedException();
}
Expand Down
Loading

0 comments on commit 5778a4b

Please sign in to comment.