1

I followed this tutorial, and created this code:

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);
        }
    }
}

Models are identical with tutorial, so I will not paste them.

My route configuration looks like this:

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

When I navigate to /comment I see this exception:

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

I tried with commenting my route specification (as there was nothing about routes in tutorial), and then error is different (throwing by Sitecore CMS itself):

The requested document was not found

Do you know how to load Sitecore context into custom Controller, and make this simple example work? I was looking everywhere but couldn't find any good answer...

2 Answers 2

4

I think this is more a Glass setup issue, rather than an MVC routing problem. To setup Glass, you need to initialise the context in your application start method in your Global.asax file.

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);

For other Glass-setup related stuff I recommend following the first tutorial on the glass.lu website. http://www.glass.lu/tutorials/glass-sitecore-mapper-tutorials/tutorial-1-setup/

2
  • I found another way to get Sitecore context, and got rid of Glass completely from the project. But it's good answer indeed.
    – Karol
    Feb 13, 2013 at 6:58
  • 1
    that link is now dead
    – Liam
    Jan 27, 2016 at 14:35
1

This method doesn't need Glass at all!

First step is to set your route in Global.asax file.

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

Notice that controller is not taken as parameter, but is fixed, to prevent handling it by Sitecore. More info here and here. Notice that there is one additional parameter - scItemPath. It contains path to item which by default will be included in page context.

Having this route our traffic from /demo is handled by DemoController and Index action. Inside this action all you need is to add is this line:

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!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.