调用共享WebMethod时出现未知Web方法异常

发布于 2024-09-07 11:03:56 字数 1439 浏览 2 评论 0原文

我正在尝试在我的网站上实施视图跟踪网络服务。我使用 JavaScript 是因为我想从我的跟踪视图中排除任何搜索机器人。问题是当我尝试使用 jQuery 发布到我创建的 Web 服务时,出现“未知的 Web 方法”错误。

$(document).ready(function() {

  $.ajax({
    type: "POST",
    url: '<%=ResolveUrl("~/WS/ItemViewTrackingService.asmx/TrackItemView") %>',
    data: "{'itemType': 'thread', 'itemId':<%=mThread.ThreadID %>}",
    contentType: "application/json; charset=utf-8"
  });

});

这是网络服务。

Imports System.Web.Services
Imports System.Web.Services.Protocols
Imports System.ComponentModel

<System.Web.Script.Services.ScriptService()> _
<System.Web.Services.WebService(Namespace:="http://tempuri.org/")> _
<System.Web.Services.WebServiceBinding(ConformsTo:=WsiProfiles.BasicProfile1_1)> _
<ToolboxItem(False)> _
Public Class ItemViewTrackingService
  Inherits System.Web.Services.WebService

  <WebMethod(EnableSession:=True)> _
  Public Shared Sub TrackItemView(ByVal itemType As String, ByVal itemId As Int32)

    If itemType.Equals("column", StringComparison.OrdinalIgnoreCase) Then
      Services.ViewTrackingService.TrackColumnView(itemId)
    ElseIf itemType.Equals("thread", StringComparison.OrdinalIgnoreCase) Then
      Services.ViewTrackingService.TrackThreadView(itemId)
    End If

  End Sub

End Class

该错误是 ASP .NET 错误:未知的 Web 方法 TrackItemView。参数名称:methodName

我已经这样做了数百次(看起来),但我只是看不出我错过了什么。我确信这是一件小事...

I'm trying to implement a view tracking web service on my website. I'm using JavaScript because I want to exclude any search bots from my tracked views. The problem is I'm getting a "Unknown web method" error when I try to use jQuery to post to the web service I've created.

$(document).ready(function() {

  $.ajax({
    type: "POST",
    url: '<%=ResolveUrl("~/WS/ItemViewTrackingService.asmx/TrackItemView") %>',
    data: "{'itemType': 'thread', 'itemId':<%=mThread.ThreadID %>}",
    contentType: "application/json; charset=utf-8"
  });

});

Here is the web service.

Imports System.Web.Services
Imports System.Web.Services.Protocols
Imports System.ComponentModel

<System.Web.Script.Services.ScriptService()> _
<System.Web.Services.WebService(Namespace:="http://tempuri.org/")> _
<System.Web.Services.WebServiceBinding(ConformsTo:=WsiProfiles.BasicProfile1_1)> _
<ToolboxItem(False)> _
Public Class ItemViewTrackingService
  Inherits System.Web.Services.WebService

  <WebMethod(EnableSession:=True)> _
  Public Shared Sub TrackItemView(ByVal itemType As String, ByVal itemId As Int32)

    If itemType.Equals("column", StringComparison.OrdinalIgnoreCase) Then
      Services.ViewTrackingService.TrackColumnView(itemId)
    ElseIf itemType.Equals("thread", StringComparison.OrdinalIgnoreCase) Then
      Services.ViewTrackingService.TrackThreadView(itemId)
    End If

  End Sub

End Class

The error is an ASP .NET error: Unknown web method TrackItemView. Parameter name: methodName

I've done this hundreds of times (seemingly), but I just can't see what I'm missing. I'm sure it's something small...

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

墨离汐 2024-09-14 11:03:56

您不能在 Web 服务中使用Shared(C# 中的static)方法。您可能会考虑在 ASPX 页面中使用静态方法作为“页面方法”。在独立的 ASMX Web 服务中,您只能使用实例方法。

You cannot use a Shared (static in C#) method in a web service. You may be thinking of the use of static methods as "page methods" in an ASPX page. In a standalone ASMX web service, you can only use instance methods.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文