自定义部分和自定义树 MVC 方法
Custom Sections & Custom Trees MVC approach
我花了好几个小时试图让一棵自定义树显示在 umbraco 的自定义部分中。 - 没有成功。
到目前为止,我已经设法创建了一个新部分,但是当我单击该部分时没有任何反应。
应该发生的是自定义树应该显示一个节点。当您单击该节点时,它应该显示一个 MVC 视图。
这是我到目前为止所做的,它基于本教程。
一个。创建栏目
{
[Application("rewards", "Rewards", "icon-gift", 15)]
public class RewardsSection: IApplication
{
}
}
乙。创建树
[Tree("rewards", "rewardsTree", "Rewards")]
[PluginController("Rewards")]
public class RewardsTree : BaseTree
{
public RewardsTree(string application)
: base(application)
{ }
protected override void CreateRootNode(ref XmlTreeNode rootNode)
{
rootNode.NodeType = "rewards";
rootNode.NodeID = "-1";
rootNode.Menu = new List<IAction> { ActionRefresh.Instance };
}
public override void Render(ref XmlTree tree)
{
var IndexNode = XmlTreeNode.Create(this);
IndexNode.NodeID = "0";
IndexNode.NodeType = "Home";
IndexNode.Text = "Home";
IndexNode.Action = "javascript:openPage('/umbraco/backoffice/Plugins/Rewards/Index');";
IndexNode.Icon = "icon-home";
IndexNode.HasChildren = false;
IndexNode.Menu = new List<IAction>();
OnBeforeNodeRender(ref tree, ref IndexNode, EventArgs.Empty);
if (IndexNode != null)
{
tree.Add(IndexNode);
OnAfterNodeRender(ref tree, ref IndexNode, EventArgs.Empty);
}
}
public override void RenderJS(ref StringBuilder Javascript)
{
var js = $"function openPage(url){{UmbClientMgr.contentFrame(url);}}";
Javascript.Append(js);
}
protected override void CreateAllowedActions(ref List<IAction> actions)
{
actions.Clear();
actions.Add(ActionNew.Instance);
actions.Add(ActionDelete.Instance);
actions.Add(ContextMenuSeperator.Instance);
actions.Add(ActionRefresh.Instance);
}
}
C。 RegisterRoutes(在 ApplicationStarted 上调用)
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute(
name: "Default",
url: "umbraco/backoffice/Plugins/{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
}
public class StartUpHandlers : ApplicationEventHandler
{
protected override void ApplicationStarted(UmbracoApplicationBase umbracoApplication, ApplicationContext applicationContext)
{
RouteConfig.RegisterRoutes(RouteTable.Routes);
}
}
D.后端控制器-返回视图我想看。
public class RewardsController : UmbracoAuthorizedController
{
public ActionResult Index()
{
return View("~/App_Plugins/Rewards/Views/RewardsHome/Index.cshtml");
}
}
那我错过了什么?
我在 RewardsTree 中放置了断点 Class 但 none 被击中了。
我还在 Application Started 上设置了断点,这些断点被击中,所以我很确定路由配置正确。
有什么我遗漏的吗?我看过其他使用 TreeController 代替 TreeBase 的示例,这让我有点困惑。
有什么想法吗? - 非常卡
我成功地在自定义部分中显示了自定义树!终于! -
我遇到的问题有两个。
在所有指导您在定义自定义树时如何执行此操作的在线教程中,一些教程继承自 BaseTree,其他教程继承自 TreeController -
我已经从 TreeController 继承了它,据我所知,使用 BaseTree 是 'old way' 的做法。
我正在 运行ning Umbraco 版本 7.6.4 程序集:1.0.6396.36621 - 我不确定旧方法是否适用于此版本,但我无法让它工作.
所以在遵循本书第 16 章(自定义部分、树和操作)中的指南之后:https://github.com/kgiszewski/LearnUmbraco7
我编译了我的项目,运行,显示了自定义部分,但没有显示自定义树,当我单击自定义部分时出现错误。
System.NullReferenceException: 对象引用未设置为对象的实例。
部分堆栈跟踪如下所示:
Umbraco.Web.Trees.ApplicationTreeExtensions.TryLoadFromControllerTree(ApplicationTree appTree, String id, FormDataCollection formCollection, HttpControllerContext controllerContext) at Umbraco.Web.Trees.ApplicationTreeController.d__17.MoveNext() --- 堆栈跟踪的结尾,从之前发生异常的位置开始抛出 -
发生这种情况的原因并不明显,但经过一些研究后我发现这是因为....................................
我正在使用一个 IoC 容器,为了让它工作,我需要确保 TreeController 已在容器中注册! - 这是任何与创建自定义树相关的文档或教程中都没有提到的内容。该文档假定您在没有 IoC 容器的情况下使用 Umbraco。
在我的例子中,我使用的是 Autofac,所以我所要做的就是将它添加到我的容器注册代码中:
builder.RegisterApiControllers(typeof(RewardsTreeController).Assembly);
突然一切正常!
真心希望这对其他人有帮助。
Umbraco (7.6.4) / Autofac.Mvc5 (4.0.2) / Autofac.WebApi2 (4.0.1) / << 这是对我有用的 nuget 包的组合。
我花了好几个小时试图让一棵自定义树显示在 umbraco 的自定义部分中。 - 没有成功。
到目前为止,我已经设法创建了一个新部分,但是当我单击该部分时没有任何反应。
应该发生的是自定义树应该显示一个节点。当您单击该节点时,它应该显示一个 MVC 视图。
这是我到目前为止所做的,它基于本教程。
一个。创建栏目
{
[Application("rewards", "Rewards", "icon-gift", 15)]
public class RewardsSection: IApplication
{
}
}
乙。创建树
[Tree("rewards", "rewardsTree", "Rewards")]
[PluginController("Rewards")]
public class RewardsTree : BaseTree
{
public RewardsTree(string application)
: base(application)
{ }
protected override void CreateRootNode(ref XmlTreeNode rootNode)
{
rootNode.NodeType = "rewards";
rootNode.NodeID = "-1";
rootNode.Menu = new List<IAction> { ActionRefresh.Instance };
}
public override void Render(ref XmlTree tree)
{
var IndexNode = XmlTreeNode.Create(this);
IndexNode.NodeID = "0";
IndexNode.NodeType = "Home";
IndexNode.Text = "Home";
IndexNode.Action = "javascript:openPage('/umbraco/backoffice/Plugins/Rewards/Index');";
IndexNode.Icon = "icon-home";
IndexNode.HasChildren = false;
IndexNode.Menu = new List<IAction>();
OnBeforeNodeRender(ref tree, ref IndexNode, EventArgs.Empty);
if (IndexNode != null)
{
tree.Add(IndexNode);
OnAfterNodeRender(ref tree, ref IndexNode, EventArgs.Empty);
}
}
public override void RenderJS(ref StringBuilder Javascript)
{
var js = $"function openPage(url){{UmbClientMgr.contentFrame(url);}}";
Javascript.Append(js);
}
protected override void CreateAllowedActions(ref List<IAction> actions)
{
actions.Clear();
actions.Add(ActionNew.Instance);
actions.Add(ActionDelete.Instance);
actions.Add(ContextMenuSeperator.Instance);
actions.Add(ActionRefresh.Instance);
}
}
C。 RegisterRoutes(在 ApplicationStarted 上调用)
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute(
name: "Default",
url: "umbraco/backoffice/Plugins/{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
}
public class StartUpHandlers : ApplicationEventHandler
{
protected override void ApplicationStarted(UmbracoApplicationBase umbracoApplication, ApplicationContext applicationContext)
{
RouteConfig.RegisterRoutes(RouteTable.Routes);
}
}
D.后端控制器-返回视图我想看。
public class RewardsController : UmbracoAuthorizedController
{
public ActionResult Index()
{
return View("~/App_Plugins/Rewards/Views/RewardsHome/Index.cshtml");
}
}
那我错过了什么?
我在 RewardsTree 中放置了断点 Class 但 none 被击中了。
我还在 Application Started 上设置了断点,这些断点被击中,所以我很确定路由配置正确。
有什么我遗漏的吗?我看过其他使用 TreeController 代替 TreeBase 的示例,这让我有点困惑。
有什么想法吗? - 非常卡
我成功地在自定义部分中显示了自定义树!终于! -
我遇到的问题有两个。
在所有指导您在定义自定义树时如何执行此操作的在线教程中,一些教程继承自 BaseTree,其他教程继承自 TreeController -
我已经从 TreeController 继承了它,据我所知,使用 BaseTree 是 'old way' 的做法。
我正在 运行ning Umbraco 版本 7.6.4 程序集:1.0.6396.36621 - 我不确定旧方法是否适用于此版本,但我无法让它工作.
所以在遵循本书第 16 章(自定义部分、树和操作)中的指南之后:https://github.com/kgiszewski/LearnUmbraco7
我编译了我的项目,运行,显示了自定义部分,但没有显示自定义树,当我单击自定义部分时出现错误。
System.NullReferenceException: 对象引用未设置为对象的实例。
部分堆栈跟踪如下所示:
Umbraco.Web.Trees.ApplicationTreeExtensions.TryLoadFromControllerTree(ApplicationTree appTree, String id, FormDataCollection formCollection, HttpControllerContext controllerContext) at Umbraco.Web.Trees.ApplicationTreeController.d__17.MoveNext() --- 堆栈跟踪的结尾,从之前发生异常的位置开始抛出 -
发生这种情况的原因并不明显,但经过一些研究后我发现这是因为....................................
我正在使用一个 IoC 容器,为了让它工作,我需要确保 TreeController 已在容器中注册! - 这是任何与创建自定义树相关的文档或教程中都没有提到的内容。该文档假定您在没有 IoC 容器的情况下使用 Umbraco。
在我的例子中,我使用的是 Autofac,所以我所要做的就是将它添加到我的容器注册代码中:
builder.RegisterApiControllers(typeof(RewardsTreeController).Assembly);
突然一切正常!
真心希望这对其他人有帮助。
Umbraco (7.6.4) / Autofac.Mvc5 (4.0.2) / Autofac.WebApi2 (4.0.1) / << 这是对我有用的 nuget 包的组合。