1

我必须向使用 Synapse 的内容管理系统的 VS 2008 (.Net 3.5) 网站添加功能。在挣扎了几个小时后,我从头开始,并在一个全新的 Web 项目中进行了以下工作。

这是客户端脚本和html:

<script type="text/javascript">
function ClickedIt() {
    $.ajax({
        type: "POST",
        url: "Default.aspx/FromClient",
        data: "{}",
        contentType: "application/json; charset=utf-8",
        dataType: "json",
        success: function(msg) {
            alert(msg.d);
        }
    });
} // ClickedIt
function ClickedItDeeper() {
    $.ajax({
        type: "POST",
        url: "/Deeper/ActionsController.aspx/FromClient",
        data: "{}",
        contentType: "application/json; charset=utf-8",
        dataType: "json",
        success: function(msg) {
            alert(msg.d);
        }
    });
} // ClickedItDeeper
</script>

<asp:Button ID="btnClickMe" runat="server" Text="Click Me - Server" />&nbsp;&nbsp;
<asp:Button ID="btnViaClient" runat="server" Text="Click Me - Client" OnClientClick="ClickedIt(); return false;" />&nbsp;&nbsp;
<asp:Button ID="btnDeeper" runat="server" Text="Click Me - Deeper" OnClientClick="ClickedItDeeper(); return false;" />

更好的是,我必须用 VB 编写它,所以这是我调用的两个不同的测试。一个是我正在运行的实际页面(Default.aspx),另一个是我喜欢将方法保持在一起的方式(ActionsController.aspx):

Imports System.Web.Services

  Partial Public Class _Default
  Inherits System.Web.UI.Page

Protected Sub btnClickMe_Click(ByVal sender As Object, ByVal e As EventArgs) Handles btnClickMe.Click
    Me.lblResult.Text = "From the Server Side: " + DateTime.Now.ToString()
End Sub

<WebMethod()> _
Public Shared Function FromClient() As String
    Return "Via the Client: " + DateTime.Now.ToString()
End Function

End Class

ActionsController 是:

Imports System.Web.Services

Partial Public Class ActionsController
Inherits System.Web.UI.Page

<WebMethod()> _
Public Shared Function FromClient() As String
    Return "Via the Deeper Client: " + DateTime.Now.ToString()
End Function

End Class

在那个孤立的站点中,上述所有工作都像冠军一样。但是,当我尝试在这个 CMS 站点中做同样的事情时,我得到一个:parseerror: invalid character。在查看了许多文章之后(没有人修复它),有人提到也许 Telerik 甚至 CMS 软件正在覆盖 JSON.parse,看起来好像是这样。我强迫它不使用另一个,现在它正在使用 json2.js。当我放一个:

alert(text);

就在 json2.js 文件中:

JSON.parse = function(text, reviver) {

它向我显示了整个 html 页面内容,这是我用这个调用它时找不到的页面:

$.ajax({
type: "POST",
url: "ActionsController.aspx/FromClient",
data: "{}",
contentType: "application/json",
dataType: "json",
success: function(msg) {
    $("#lblResult").html(msg);
    alert("success");
},
error: function(xhr, textStatus, errorThrown) {
    alert("Ajax error on " + this.url +
        "\nStatus: " + textStatus +
        "\nError: " + errorThrown +
        "\nText: " + xhr.statusText +
        "\nxhrStatus: " + xhr.status
    );
}
});

给我一个 SyntaxError: JSON.parse 错误,但文本值为 OK,状态为 200。

我已将 URL 更改为没有前导 / 并在开头添加了 en-US 和没有前导斜杠都无济于事。我不确定找不到的页面是否合法。最初,ActionsController 页面位于一个文件夹中。我已经把它移到了网站的根目录。

这是我在VS2010上做过很多次的事情。它可以在 VS2008 中运行,但我认为有什么东西妨碍了它,要么将一些东西附加到 URL,要么感谢这个 CMS。当它出错时,我确实让它传递给我 this.url,它是:/ActionsController.aspx/FromClient 这样对我有用。

这是损坏站点的实际 ActionsController 代码:

Imports System.Web.Services

Partial Public Class ActionsController
Inherits System.Web.UI.Page

<WebMethod()> _
 Public Shared Function FromClient() As String
    Return "Via the Deeper Client: " + DateTime.Now.ToString()
End Function

<WebMethod()> _
Public Shared Function Multiply(ByVal Input As Integer) As String
    Return "Via the Deeper Client: " + DateTime.Now.ToString()
End Function

End Class

再过一个小时,这将是一整天的搜索、调整和测试,这些东西可能很简单,但隐藏在 Telerik 和突触的一层又一层“功能”之下。

请让我开心!

4

1 回答 1

0

好的,在与开发人员交谈后,我最终获得了足够的信息来解决这个问题。基本上,我必须做两件事才能让它发挥作用。一,我必须覆盖 Global.asax 文件中的 BeginRequest,然后将一些值添加到 web.config。

万一其他人遇到这样的问题,这里是完整的结果。

在 .aspx 页面上,这里是 jQuery ajax 调用:

function LoadAllScannedDocumentCounts() {
    var labels = $("[data-DocumentFolder]");

    $.each(labels, function(index, label) {
        var ComplaintNumber = $(this).attr("data-DocumentFolder");
        $(this).prepend("<img id='spinImage" + ComplaintNumber + "' src='/ComplaintsReports/Images/spinner.gif' />");

        $.ajax({
            url: "/Exclusions/ActionsController.aspx/GetScannedDocumentCount",
            type: "POST",
            data: "{ " +
                    "       'ComplaintNumber': '" + ComplaintNumber + "'" +
                " }",
            contentType: "application/json",
            dataType: "json",
            success: function(data) {
                $(label).text(data.d);

                if (data.d > 0)
                    $(label).addClass("badge badge-info");
                else
                    $(label).addClass("badge");
            }, // Success
            error: function(XMLHttpRequest, textStatus, errorThrown) {
                alert("Error: " + errorThrown + "\nURL: " + this.url);
            } // Error
        }); // Ajax
    }); // foreach of the checked check boxes
} // LoadAllScannedDocumentCounts - Method

web.config 添加:

<system.web.extensions>
    <scripting>
        <webServices>
            <authenticationService enabled="true" />
        </webServices>
    </scripting>
</system.web.extensions>

Global.asax 文件:

导入 System.Web.SessionState

Public Class Global_asax
Inherits SynapseBaseSite46.Global

Sub Application_BeginRequest(ByVal sender As Object, ByVal e As EventArgs) Handles MyBase.BeginRequest
    Dim app As HttpApplication = DirectCast(sender, HttpApplication)
    Dim con As HttpContext = app.Context

    ' Skip any calls to the ActionsController
    If (Not con.Request.FilePath.ToLower().Contains("exclusions/actionscontroller.aspx")) Then
        MyBase.Application_BeginRequest(sender, e)
    End If
End Sub

End Class

最后是带有 WebMethods 的“ActionsController”页面:

Imports System.Web.Services
Imports System.IO

Partial Public Class ActionsController
Inherits System.Web.UI.Page

<WebMethod()> _
 Public Shared Function GetScannedDocumentCount(ByVal ComplaintNumber As String) As Integer
    Dim Result As Integer = 0
    Dim SearchPath As String = ConfigurationManager.AppSettings("ScannedImagesShare") & "\"

    Try
        For Each dir As DirectoryInfo In New DirectoryInfo(SearchPath & ComplaintNumber.Replace("-", "")).GetDirectories()
            Dim files As FileInfo() = dir.GetFiles("*.pdf")
            Result += files.Count()
        Next
    Catch dex As DirectoryNotFoundException
        ' do nothing
    Catch ex As Exception

    End Try

    Return Result
 End Function

End Class
于 2013-04-11T14:58:53.493 回答