2017-07-20 11:21:28 +02:00
|
|
|
|
using System.Web.Mvc;
|
2017-05-30 18:13:11 +02:00
|
|
|
|
using Umbraco.Web.Composing;
|
2016-02-16 11:17:47 +01:00
|
|
|
|
|
|
|
|
|
|
namespace Umbraco.Web.Mvc
|
|
|
|
|
|
{
|
|
|
|
|
|
public static class ControllerContextExtensions
|
|
|
|
|
|
{
|
|
|
|
|
|
/// <summary>
|
2016-06-09 11:57:13 +02:00
|
|
|
|
/// Gets the Umbraco context from a controller context hierarchy, if any, else the 'current' Umbraco context.
|
2016-02-16 11:17:47 +01:00
|
|
|
|
/// </summary>
|
2016-06-09 11:57:13 +02:00
|
|
|
|
/// <param name="controllerContext">The controller context.</param>
|
|
|
|
|
|
/// <returns>The Umbraco context.</returns>
|
2020-02-09 18:53:37 +01:00
|
|
|
|
public static IUmbracoContext GetUmbracoContext(this ControllerContext controllerContext)
|
2016-02-16 11:17:47 +01:00
|
|
|
|
{
|
2016-06-09 11:57:13 +02:00
|
|
|
|
var o = controllerContext.GetDataTokenInViewContextHierarchy(Core.Constants.Web.UmbracoContextDataToken);
|
2020-02-09 18:53:37 +01:00
|
|
|
|
return o != null ? o as IUmbracoContext : Current.UmbracoContext;
|
2016-02-16 11:17:47 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
2016-06-09 11:57:13 +02:00
|
|
|
|
/// Recursively gets a data token from a controller context hierarchy.
|
2016-02-16 11:17:47 +01:00
|
|
|
|
/// </summary>
|
2016-06-09 11:57:13 +02:00
|
|
|
|
/// <param name="controllerContext">The controller context.</param>
|
|
|
|
|
|
/// <param name="dataTokenName">The name of the data token.</param>
|
|
|
|
|
|
/// <returns>The data token, or null.</returns>
|
2020-11-03 14:14:01 +01:00
|
|
|
|
/// MIGRATED TO NETCORE AS EXTENSION ON ActionContext
|
2016-02-16 11:17:47 +01:00
|
|
|
|
internal static object GetDataTokenInViewContextHierarchy(this ControllerContext controllerContext, string dataTokenName)
|
|
|
|
|
|
{
|
2016-06-09 11:57:13 +02:00
|
|
|
|
var context = controllerContext;
|
|
|
|
|
|
while (context != null)
|
2016-02-16 11:17:47 +01:00
|
|
|
|
{
|
2016-06-09 11:57:13 +02:00
|
|
|
|
object token;
|
|
|
|
|
|
if (context.RouteData.DataTokens.TryGetValue(dataTokenName, out token))
|
|
|
|
|
|
return token;
|
|
|
|
|
|
context = context.ParentActionViewContext;
|
2016-02-16 11:17:47 +01:00
|
|
|
|
}
|
|
|
|
|
|
return null;
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
2017-07-20 11:21:28 +02:00
|
|
|
|
}
|