Moves strings to constants, adds ControllerContextExtensions to get the UmbracoContext from the hierarchy of ControllerContext's, changes RenderModelBinder to use this method to get the UmbracoContext, changes UmbracoViewPageOfTModel to use this method to get the UmbracoContext, adds RouteDataExtensions to get the UmbracoContext from routedata, adds extension methods on the HttpContext to get the UmbracoContext from it.
This commit is contained in:
50
src/Umbraco.Web/Mvc/ControllerContextExtensions.cs
Normal file
50
src/Umbraco.Web/Mvc/ControllerContextExtensions.cs
Normal file
@@ -0,0 +1,50 @@
|
||||
using System.Web.Mvc;
|
||||
|
||||
namespace Umbraco.Web.Mvc
|
||||
{
|
||||
public static class ControllerContextExtensions
|
||||
{
|
||||
/// <summary>
|
||||
/// Tries to get the Umbraco context from the whole ControllerContext hierarchy based on data tokens and if that fails
|
||||
/// it will attempt to fallback to retrieving it from the HttpContext.
|
||||
/// </summary>
|
||||
/// <param name="controllerContext"></param>
|
||||
/// <returns></returns>
|
||||
public static UmbracoContext GetUmbracoContext(this ControllerContext controllerContext)
|
||||
{
|
||||
var umbCtx = controllerContext.RouteData.GetUmbracoContext();
|
||||
if (umbCtx != null) return umbCtx;
|
||||
|
||||
if (controllerContext.ParentActionViewContext != null)
|
||||
{
|
||||
//recurse
|
||||
return controllerContext.ParentActionViewContext.GetUmbracoContext();
|
||||
}
|
||||
|
||||
//fallback to getting from HttpContext
|
||||
return controllerContext.HttpContext.GetUmbracoContext();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Find a data token in the whole ControllerContext hierarchy of execution
|
||||
/// </summary>
|
||||
/// <param name="controllerContext"></param>
|
||||
/// <param name="dataTokenName"></param>
|
||||
/// <returns></returns>
|
||||
internal static object GetDataTokenInViewContextHierarchy(this ControllerContext controllerContext, string dataTokenName)
|
||||
{
|
||||
if (controllerContext.RouteData.DataTokens.ContainsKey(dataTokenName))
|
||||
{
|
||||
return controllerContext.RouteData.DataTokens[dataTokenName];
|
||||
}
|
||||
|
||||
if (controllerContext.ParentActionViewContext != null)
|
||||
{
|
||||
//recurse!
|
||||
return controllerContext.ParentActionViewContext.GetDataTokenInViewContextHierarchy(dataTokenName);
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user