无法在Sitecore中访问WebMethod [英] WebMethod not accessible in Sitecore

查看:151
本文介绍了无法在Sitecore中访问WebMethod的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在工作的网站出现问题. 从JavaScript函数调用WebMethod:

I got a problem for a website I was working on. A WebMethod is called from a JavaScript function:

var ajaxOptions = {
    url: "/layouts/foobar/Foo.aspx/GetBar"
}
$.ajax(ajaxOptions).done(function(result) { 
    loadResult(result, a);
});

Foo.Aspx中的GetBar方法如下:

The method GetBar in Foo.Aspx looks like this:

[WebMethod]
[ScriptMethod(ResponseFormat = ResponseFormat.Json)]
public static object GetBar()
{
    return FoobarManager.GetItems();
}

该函数返回有效的JSON对象. 在FoobarManager中使用Sitecore.示例:

The function returns a valid JSON object. Sitecore is used in the FoobarManager. Example:

Context.Database.GetItem("/sitecore/content/foobar");

在开发和测试环境中,一切运行得非常顺利,没有任何问题.部署到暂存环境后,我看到GetBar函数的响应是我们的自定义404页面.我认为这是因为Sitecore试图解析路径"/layouts/foobar/Foo.aspx/GetBar"而失败.

On the development and test environment things ran very smoothly with no problems what so ever. After the deployment to the staging environment I saw that the response of the GetBar function was our custom 404 page. I figured this was because Sitecore was trying to resolve the path "/layouts/foobar/Foo.aspx/GetBar" and failed.

由于它可以在D和T环境下工作,因此一定是配置问题. 我将GetBar url添加到了IgnoreUrlPrefixes设置中. 这在ajax调用中导致500响应:Sitecore.Context.Database为null. 我认为它与ItemResolver有关,但是我不明白为什么它在D和T上起作用,但不适用于A.

Since it worked on the D and T environment, it must be a configuration problem. I added the GetBar url to the IgnoreUrlPrefixes setting. This resulted in a 500 response in the ajax call: the Sitecore.Context.Database was null. I figured it had something to do with the ItemResolver but I can't understand WHY it worked on D and T but not on A.

我该怎么做才能正确解析网址?

What can I do to resolve the url correctly?

推荐答案

您有两个矛盾的地方.如果您必须将/layouts/foobar/Foo.aspx路径添加到必须执行的IgnoreUrlPrefixes设置中,则Sitecore将不会在其中运行其上下文.这意味着您的代码Context.Database...无效.我建议您按名称获取数据库.您是否需要发布的"Web"数据库?您需要弄清楚您需要哪一个,但是像这样:

You have two conflicting things. If you add the /layouts/foobar/Foo.aspx path to the IgnoreUrlPrefixes setting which you MUST do (as you did), then Sitecore will not run its context in that. That means, your code Context.Database... is invalid. I recommend you get the DB by name. Do you need the published "web" DB? You'll need to figure out which one you need but something like this:

var db = Sitecore.Configuration.Factory.GetDatabase("web"); // assumes you need the web DB
var foobar = db.GetItem("/sitecore/content/foobar");

TL; DR :没有上下文,因此您无法从中获取数据库.

TL;DR: There is no context so you can't get the DB from it.

这篇关于无法在Sitecore中访问WebMethod的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆