调用共享WebMethod时出现未知Web方法异常
我正在尝试在我的网站上实施视图跟踪网络服务。我使用 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您不能在 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.