Asp.net mvc 3 自定义控制器中未加载Sitecore上下文
我遵循这一点,创建了以下代码:Asp.net mvc 3 自定义控制器中未加载Sitecore上下文,asp.net-mvc-3,sitecore,sitecore6,Asp.net Mvc 3,Sitecore,Sitecore6,我遵循这一点,创建了以下代码: 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; na
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);
}
}
}
当我导航到/comment时,我看到此异常:
Glass.Sitecore.Mapper.MapperException: Context has not been loaded
我试着评论我的路线规范,因为教程中没有关于路线的内容,然后错误就不同于Sitecore CMS本身:
找不到请求的文档
您知道如何将Sitecore上下文加载到自定义控制器中,并使这个简单的示例正常工作吗?我到处找,但找不到任何好的答案…我认为这更多的是玻璃安装问题,而不是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.lu网站上的第一个教程。
我认为这更多的是玻璃安装问题,而不是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.lu网站上的第一个教程。
这种方法根本不需要玻璃 第一步是在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);
请注意,控制器不是作为参数,而是固定的,以防止Sitecore处理它。更多信息和信息。请注意,还有一个额外的参数—scItemPath。它包含默认情况下将包含在页面上下文中的项的路径
通过此路由,DemoController和索引操作将处理来自/演示的流量。在此操作中,您只需添加以下行:
Sitecore.Data.Items.Item item = Sitecore.Mvc.Presentation.PageContext.Current.Item;
item变量将包含scItemPath指向的Sitecore项目
仅此而已-现在应该可以很好地工作了-希望能有所帮助 这种方法根本不需要玻璃 第一步是在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);
请注意,控制器不是作为参数,而是固定的,以防止Sitecore处理它。更多信息和信息。请注意,还有一个额外的参数—scItemPath。它包含默认情况下将包含在页面上下文中的项的路径
通过此路由,DemoController和索引操作将处理来自/演示的流量。在此操作中,您只需添加以下行:
Sitecore.Data.Items.Item item = Sitecore.Mvc.Presentation.PageContext.Current.Item;
item变量将包含scItemPath指向的Sitecore项目
仅此而已-现在应该可以很好地工作了-希望能有所帮助 我找到了另一种获取Sitecore上下文的方法,并将Glass完全从项目中移除。但这确实是一个好答案。这个链接现在已经失效了。我找到了另一种获取Sitecore上下文的方法,并且完全从项目中删除了Glass。但这确实是一个好答案。这种联系现在已经消失了