通过Ajax调用Sitecore ItemWebApi时出现404错误

时间:2016-01-22 09:34:30

标签: asp.net-mvc sitecore

我试图按照此视频中的教程: https://www.youtube.com/watch?v=PfnTJsUKxiY 还有这个博客: https://cmsview.wordpress.com/tag/sitecore-speak-for-beginners/

这些教程的示例都启动了对URL /api/sitecore/{controller}/{action}的ajax调用。它是重定向到该控制器操作的默认Sitecore行为还是我需要自己配置路由?我不能像教程所示那样使用这个url。每次都是404错误。我正在使用Sitecore 8。

感谢。

编辑: 下面是我的控制器类中的代码

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace SwireCitygate.Web.Controllers
{
    public class MembersBehaviourReportsController : Controller
    {
        // GET: Default
        public ActionResult GetText()
        {
            string text = "Hello World";

            return Json(text, JsonRequestBehavior.AllowGet);
        }
    }
}

继承System.Web.Mvc.Controller

和JS文件:

define(["sitecore"], function (Sitecore) {
    var model = Sitecore.Definitions.Models.ControlModel.extend({
        initialize: function (options) {
            this._super();

            this.set("output", null);
            this.GetText(this);
        },
        GetText: function (app) {
            jQuery.ajax({
                type: "GET",
                dataType: "json",
                url: "/api/sitecore/MembersBehaviourReports/GetText",
                cache: false,
                success: function (data) {
                    app.set("output", data);
                },
                error: function () {
                    console.log("There was an error in GetText() function!");
                }
            });
        }
    });

    var view = Sitecore.Definitions.Views.ControlView.extend({
        initialize: function (options) {
            this._super();

            this.set("output", null);
        }
    });

    Sitecore.Factories.createComponent("MembersBehaviourReportsDataSource", model, view, ".sc-MembersBehaviourReportsDataSource");
});

2 个答案:

答案 0 :(得分:1)

如果没有看到您的代码,我会说您应该确保您的Controller继承自MVC控制器System.Web.Controller而不是WebApi控制器System.Web.Http.ApiController

如果您使用的是WebApi控制器,则需要设置自定义路线,而不是使用/ api / sitecore /...

答案 1 :(得分:0)

毕竟,问题是由Sitecore配置引起的。 ItemWebAPI未启用。

<site name="website">
    <patch:attribute name="itemwebapi.mode">Off</patch:attribute>
    <patch:attribute name="itemwebapi.access">ReadOnly</patch:attribute>
    <patch:attribute name="itemwebapi.allowanonymousaccess">false</patch:attribute>
</site>

我将itemwebapi.mode设置为StandardSecurity,将itemwebapi.allowanonymousaccess设置为true,然后它神奇地工作。