页面方法无法从asmx文件而不是aspx任何建议?

时间:2012-01-25 22:07:07

标签: vb.net asmx webmethod

我使用简单的json方法来调用webmethod 但它适用于aspx文件,即url:

'myclass.aspx/myfunction'

但是如果我在asmx文件中放入相同的函数并将url更改为asmx,它就无法工作。

还有什么必须要做才能启用asmx服务吗?

asmx with vb code:// this works

<%@ WebService Language="VB" Class="WebService" %>

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

' To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line.
<System.Web.Script.Services.ScriptService()> _
<WebService(Namespace:="http://tempuri.org/")> _
<WebServiceBinding(ConformsTo:=WsiProfiles.BasicProfile1_1)> _
Public Class WebService
    Inherits System.Web.Services.WebService

    <System.Web.Services.WebMethod()> _
    <System.Web.Script.Services.ScriptMethod()> _
    Public Function abc(ByVal args As String) As String
               Return returnValue
    End Function

End Class
带代码隐藏文件的

asmx

<%@ WebService Language="VB"  CodeBehind="default.vb" Class="default" %> //this doesnt work

文件背后的代码

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

' To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line.
<System.Web.Script.Services.ScriptService()> _
<WebService(Namespace:="http://tempuri.org/")> _
<WebServiceBinding(ConformsTo:=WsiProfiles.BasicProfile1_1)> _
<Global.Microsoft.VisualBasic.CompilerServices.DesignerGenerated()> _
Public Class mintnow_default
    Inherits System.Web.Services.WebService

    <System.Web.Services.WebMethod()> _
    <System.Web.Script.Services.ScriptMethod()> _
    Public Function abc(ByVal args As String) As String
               Return returnValue
    End Function

End Class

2 个答案:

答案 0 :(得分:1)

如果您希望能够使用JSON请求调用服务,则需要使用<ScriptService>属性修饰服务:

' you need to add the ScriptService attribute here
<System.Web.Script.Services.ScriptService>
<System.Web.Services.WebService(Namespace:="http://tempuri.org/")> _
<System.Web.Services.WebServiceBinding(ConformsTo:=WsiProfiles.BasicProfile1_1)> _
Public Class WebService1
    Inherits System.Web.Services.WebService

    <WebMethod()> _
    Public Function MyFunction() As String
       Return "Hello World"
    End Function

End Class

答案 1 :(得分:0)

您必须从服务方法中删除Shared关键字。