using System.Web.Mvc;
using Umbraco.Web.Composing;
namespace Umbraco.Web.Mvc
{
public static class ControllerContextExtensions
{
///
/// Gets the Umbraco context from a controller context hierarchy, if any, else the 'current' Umbraco context.
///
/// The controller context.
/// The Umbraco context.
public static IUmbracoContext GetUmbracoContext(this ControllerContext controllerContext)
{
var o = controllerContext.GetDataTokenInViewContextHierarchy(Core.Constants.Web.UmbracoContextDataToken);
return o != null ? o as IUmbracoContext : Current.UmbracoContext;
}
///
/// Recursively gets a data token from a controller context hierarchy.
///
/// The controller context.
/// The name of the data token.
/// The data token, or null.
/// MIGRATED TO NETCORE AS EXTENSION ON ActionContext
internal static object GetDataTokenInViewContextHierarchy(this ControllerContext controllerContext, string dataTokenName)
{
var context = controllerContext;
while (context != null)
{
object token;
if (context.RouteData.DataTokens.TryGetValue(dataTokenName, out token))
return token;
context = context.ParentActionViewContext;
}
return null;
}
}
}