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

これは、MVCルーティングの問題というよりも、Glassのセットアップの問題だと思います。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.luWebサイトの最初のチュートリアルに従うことをお勧めします。 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 による処理を防ぐために固定されていることに注意してください。詳細はこちらこちら。追加のパラメーターが 1 つあることに注意してくださいscItemPath。デフォルトでページコンテキストに含まれるアイテムへのパスが含まれています。

このルートからのトラフィックは、アクション/demoによって処理されます。このアクション内に追加する必要があるのは、次の行だけです。DemoControllerIndex

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

item変数には、 が指す Sitecore アイテムが含まれますscItemPath

これですべてです - 今はうまくいくはずです - お役に立てば幸いです!

于 2013-02-19T04:02:11.997 に答える