【问题标题】:Custom Sections & Custom Trees MVC approach自定义部分和自定义树 MVC 方法
【发布时间】:2017-08-01 14:40:15
【问题描述】:

我花了好几个小时试图让自定义树显示在 umbraco 的自定义部分中。 - 没有成功。

到目前为止,我已经设法创建了一个新部分,但是当我单击该部分时,没有任何反应。

应该发生的是应该用一个节点显示自定义树。当您单击该节点时,它应该显示一个 MVC 视图。

这是我到目前为止所做的,它基于本教程。

http://www.jondjones.com/learn-umbraco-cms/umbraco-developers-guide/customising-umbraco-ui/how-to-display-an-mvc-view-in-the-umbraco-backend

A.创建部分

{
    [Application("rewards", "Rewards", "icon-gift", 15)]
    public class RewardsSection: IApplication
    {
    }
}

B.创建树

[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 类中设置了断点,但没有一个被击中。

我还在 Application Started 上设置了断点,并且这些断点被击中,所以我很确定路由配置正确。

我有什么遗漏的吗?我看过其他使用 TreeBase 的 TreeController 的示例,这让我有点困惑。

有什么想法吗? - 非常卡住

【问题讨论】:

    标签: asp.net-mvc model-view-controller umbraco umbraco7


    【解决方案1】:

    我已设法在自定义部分中显示自定义树!终于! -

    我遇到的问题有两个。

    在所有在线教程中,这些教程将引导您了解如何在定义自定义树时执行此操作,一些教程从 BaseTree 继承,而其他教程从 TreeController 继承 -

    我已经让它从 TreeController 继承,据我了解,使用 BaseTree 是“旧方式”。

    我正在运行 Umbraco 版本 7.6.4 程序集:1.0.6396.36621 - 我不确定旧方法是否可以使用此版本,但我无法让它工作。

    所以在遵循本书第 16 章(自定义部分、树和操作)中的指南之后:https://github.com/kgiszewski/LearnUmbraco7

    我让我的项目编译并运行,自定义部分出现了,但自定义树没有,当我单击自定义部分时,出现错误。

    System.NullReferenceException:对象引用未设置为对象的实例。

    部分堆栈跟踪如下所示:

    Umbraco.Web.Trees.ApplicationTreeController.d__17.MoveNext() 处的 Umbraco.Web.Trees.ApplicationTreeExtensions.TryLoadFromControllerTree(ApplicationTree appTree, String id, FormDataCollection formCollection, HttpControllerContext controllerContext) --- 堆栈跟踪从上一个位置结束抛出异常 -

    为什么会发生这种情况并不明显,但经过一番研究后,我发现这是因为............

    我正在使用 IoC 容器,为此我需要确保 TreeController 已在容器中注册! - 这是任何与创建自定义树相关的文档或教程中未提及的内容。该文档假设您使用 Umbraco 时没有 IoC 容器。

    就我而言,我使用的是 Autofac,所以我所要做的就是将其添加到我的容器注册代码中:

    builder.RegisterApiControllers(typeof(RewardsTreeController).Assembly);
    

    突然间一切正常!

    真的希望这对其他人有所帮助。

    Umbraco (7.6. 4) / Autofac.Mvc5 (4.0.2) / Autofac.WebApi2 (4.0.1) /

    【讨论】:

    • 是的!!!!这正是我所苦苦挣扎的!我也花了好几个小时试图找出导致这个错误的原因!
    • :-) - 很高兴我能帮上忙!
    • 我遇到了同样的问题,使用 DI,并且发现(在 dotPeek 插件反汇编之后)每个自定义的或来自插件的树控制器都需要在 DI 中单独注册:)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-10-20
    • 1970-01-01
    • 2013-12-24
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多