mirror of
https://github.com/OrchardCMS/Orchard.git
synced 2025-10-15 19:54:57 +08:00
Unblocking the use of Html.RenderAction and Html.Action in Orchard views
--HG-- branch : dev
This commit is contained in:
@@ -9,13 +9,10 @@ namespace Orchard.Mvc.Html {
|
|||||||
/// <see cref="http://en.wikipedia.org/wiki/Harry_Houdini"/>
|
/// <see cref="http://en.wikipedia.org/wiki/Harry_Houdini"/>
|
||||||
/// <returns>himself</returns>
|
/// <returns>himself</returns>
|
||||||
public static TService Resolve<TService>(this HtmlHelper html) {
|
public static TService Resolve<TService>(this HtmlHelper html) {
|
||||||
var workContextAccessor = html.ViewContext.RouteData.DataTokens["IWorkContextAccessor"] as IWorkContextAccessor;
|
var workContext = html.ViewContext.RequestContext.GetWorkContext();
|
||||||
if (workContextAccessor == null)
|
|
||||||
throw new ApplicationException("Unable to resolve");
|
|
||||||
|
|
||||||
var workContext = workContextAccessor.GetContext(html.ViewContext.HttpContext);
|
|
||||||
if (workContext == null)
|
if (workContext == null)
|
||||||
throw new ApplicationException("Unable to resolve");
|
return default(TService);
|
||||||
|
|
||||||
return workContext.Resolve<TService>();
|
return workContext.Resolve<TService>();
|
||||||
}
|
}
|
||||||
|
@@ -10,27 +10,48 @@ namespace Orchard {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public static WorkContext GetWorkContext(this RequestContext requestContext) {
|
public static WorkContext GetWorkContext(this RequestContext requestContext) {
|
||||||
if (requestContext == null) {
|
if (requestContext == null)
|
||||||
return null;
|
return null;
|
||||||
}
|
|
||||||
|
|
||||||
var routeData = requestContext.RouteData;
|
var routeData = requestContext.RouteData;
|
||||||
object value;
|
if (routeData == null || routeData.DataTokens == null)
|
||||||
if (routeData == null ||
|
|
||||||
routeData.DataTokens == null ||
|
|
||||||
!routeData.DataTokens.TryGetValue("IWorkContextAccessor", out value) ||
|
|
||||||
!(value is IWorkContextAccessor)) {
|
|
||||||
return null;
|
return null;
|
||||||
|
|
||||||
|
object workContextValue;
|
||||||
|
if (!routeData.DataTokens.TryGetValue("IWorkContextAccessor", out workContextValue)) {
|
||||||
|
workContextValue = FindWorkContextInParent(routeData);
|
||||||
}
|
}
|
||||||
|
|
||||||
var workContextAccessor = (IWorkContextAccessor)value;
|
if (!(workContextValue is IWorkContextAccessor))
|
||||||
|
return null;
|
||||||
|
|
||||||
|
var workContextAccessor = (IWorkContextAccessor)workContextValue;
|
||||||
return workContextAccessor.GetContext(requestContext.HttpContext);
|
return workContextAccessor.GetContext(requestContext.HttpContext);
|
||||||
}
|
}
|
||||||
|
|
||||||
public static WorkContext GetWorkContext(this ControllerContext controllerContext) {
|
private static object FindWorkContextInParent(RouteData routeData) {
|
||||||
if (controllerContext == null) {
|
object parentViewContextValue;
|
||||||
|
if (!routeData.DataTokens.TryGetValue("ParentActionViewContext", out parentViewContextValue)
|
||||||
|
|| !(parentViewContextValue is ViewContext)) {
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
var parentRouteData = ((ViewContext)parentViewContextValue).RouteData;
|
||||||
|
if (parentRouteData == null || parentRouteData.DataTokens == null)
|
||||||
|
return null;
|
||||||
|
|
||||||
|
object workContextValue;
|
||||||
|
if (!parentRouteData.DataTokens.TryGetValue("IWorkContextAccessor", out workContextValue)) {
|
||||||
|
workContextValue = FindWorkContextInParent(parentRouteData);
|
||||||
|
}
|
||||||
|
|
||||||
|
return workContextValue;
|
||||||
|
}
|
||||||
|
|
||||||
|
public static WorkContext GetWorkContext(this ControllerContext controllerContext) {
|
||||||
|
if (controllerContext == null)
|
||||||
|
return null;
|
||||||
|
|
||||||
return WorkContextExtensions.GetWorkContext(controllerContext.RequestContext);
|
return WorkContextExtensions.GetWorkContext(controllerContext.RequestContext);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user