Sitecore上下文未在自定义控制器中加载

时间:2013-02-12 03:28:00

标签: asp.net-mvc-3 sitecore sitecore6

我遵循了这个tutorial,并创建了这段代码:

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上下文加载到自定义Controller中,并使这个简单的示例有效吗?我到处寻找,但找不到任何好的答案......

2 个答案:

答案 0 :(得分: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/

答案 1 :(得分: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处理它。更多信息herehere。请注意,还有一个附加参数 - scItemPath。它包含项目的路径,默认情况下将包含在页面上下文中。

通过此路线,我们/demo的流量由DemoControllerIndex操作处理。在这个动作中,您只需添加以下行:

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

item变量将包含scItemPath指向的Sitecore项目。

这就是全部 - 它现在应该运作良好 - 希望它有所帮助!