1

我遵循了本教程,并创建了以下代码:

using Glass.Sitecore.Mapper;
using Sitecore.Mvc.Controllers;
using Sitecore.SecurityModel;
using SitecoreCMSMVCBase.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace SitecoreCMSMVCBase.Controllers
{
    public class CommentController : SitecoreController
    {
        ISitecoreContext _context;
        ISitecoreService _master;

        public CommentController()
            : this(
            new SitecoreContext(),
            new SitecoreService("master"))
        {

        }
        /// <summary>
        /// This constructor can be used with dependency injection or unit testing
        /// </summary>
        public CommentController(ISitecoreContext context, ISitecoreService master)
        {
            _context = context;
            _master = master;
        }

        [HttpGet]
        public override ActionResult Index()
        {
            var model = _context.GetCurrentItem<CommentPage>();
            return View(model);
        }

        [HttpPost]
        public ActionResult Index(Comment comment)
        {
            var webModel = _context.GetCurrentItem<CommentPage>();

            if (ModelState.IsValid)
            {
                var masterModel = _master.GetItem<CommentPage>(webModel.Id);

                if (masterModel.CommentFolder == null)
                {
                    CommentFolder folder = new CommentFolder();
                    folder.Name = "Comments";

                    using (new SecurityDisabler())
                    {
                        _context.Create(masterModel, folder);
                    }
                    masterModel.CommentFolder = folder;
                }

                using (new SecurityDisabler())
                {
                    comment.Name = DateTime.Now.ToString("yyyyMMddhhmmss");

                    //create the comment in the master database
                    _master.Create(masterModel.CommentFolder, comment);
                    webModel.CommentAdded = true;
                }
            }

            return View(webModel);
        }
    }
}

模型与教程相同,所以我不会粘贴它们。

我的路由配置如下所示:

routes.MapRoute(
    "CommentController", // Route name
    "Comment/{action}/{id}", // URL with parameters
    new { controller = "Comment", action = "Index", id = UrlParameter.Optional } // Parameter defaults
);

当我导航到/comment我看到这个异常:

Glass.Sitecore.Mapper.MapperException: Context has not been loaded

我尝试评论我的路由规范(因为教程中没有关于路由的内容),然后错误不同(由 Sitecore CMS 本身抛出):

未找到请求的文档

你知道如何将 Sitecore 上下文加载到自定义控制器中,并使这个简单的示例工作吗?我到处寻找,但找不到任何好的答案...

4

2 回答 2

4

我认为这更像是 Glass 设置问题,而不是 MVC 路由问题。要设置 Glass,您需要在 Global.asax 文件的应用程序启动方法中初始化上下文。

var loader = new Glass.Sitecore.Mapper.Configuration.Attributes.AttributeConfigurationLoader(
        "Glass.Sitecore.Mapper.Tutorial.Models, Glass.Sitecore.Mapper.Tutorial");

Glass.Sitecore.Mapper.Context context = new Context(loader);

对于其他与 Glass 设置相关的内容,我建议遵循 glass.lu 网站上的第一个教程。 http://www.glass.lu/tutorials/glass-sitecore-mapper-tutorials/tutorial-1-setup/

于 2013-02-12T12:58:33.177 回答
1

这种方法根本不需要 Glass!

第一步是在Global.asax文件中设置您的路线。

routes.MapRoute(
    "DemoController", // Route name
    "Demo/{action}/{param}", // URL with parameters
    new { controller = "Demo", action = "Index", param = "", scItemPath = "/sitecore/content/DemoHomePage" } // Parameter defaults
);

请注意,控制器不是作为参数,而是固定的,以防止 Sitecore 对其进行处理。更多信息在这里这里。请注意,还有一个附加参数 - scItemPath。它包含默认情况下将包含在页面上下文中的项目的路径。

有了这条路线,我们的流量就由动作/demo来处理。在此操作中,您只需添加以下行:DemoControllerIndex

Sitecore.Data.Items.Item item = Sitecore.Mvc.Presentation.PageContext.Current.Item;

item variable will contain your Sitecore item pointed by scItemPath.

And that's all - it should work well now - hope it helps!

于 2013-02-19T04:02:11.997 回答