WebMethod を使用できるようにするサーバー ユーザー コントロールを作成しました。Web メソッドがメイン アプリケーション (ASMX ファイルとして) にある場合、正常に動作します。問題は、このメソッドをユーザー コントロールと同じプロジェクト ライブラリに含めて、DLL をスタンドアロン プロジェクトとして配布できるようにすることです。このプロジェクトはクラス ライブラリであるため、Web サービスを .ASMX ではなく VB ファイルにする必要がありました。.VB ファイルで Web メソッドを呼び出そうとしても、何も起こらないように見えます (エラーは発生しませんが、Web メソッドのブレーク ポイントには到達しません)。以下は、コントロールの作成方法の例です。
myClass.VB の Web メソッド:
<System.Web.Script.Services.ScriptService()> _
<WebService(Namespace:="http://tempuri.org/")> _
<WebServiceBinding(ConformsTo:=WsiProfiles.BasicProfile1_1)> _
Public Class myClass
Inherits System.Web.Services.WebService
<WebMethod()> _
Public Function TestMethod(ByVal prefixText As String) As String
return "Hello World"
end Function
私のサーバーコントロールは、miniActiveDirectorySearch.VB で次のように設定されています。
Public Class miniActiveDirectorySearch
Inherits WebControl
Private Sub attachWebResources()
ScriptManager.RegisterClientScriptResource(Me, Me.GetType, "myScripts.js")
ScriptManager.RegisterClientScriptResource(Me, Me.GetType, "jquery-1.4.1.min.js")
End Sub
Protected Overrides Sub CreateChildControls()
createDynamicControls()
MyBase.CreateChildControls()
End Sub
Protected Overrides Sub OnInit(ByVal e As System.EventArgs)
attachWebResources()
MyBase.OnInit(e)
End Sub
Private Sub createDynamicControls()
Controls.Clear()
Try
tblMainLayout = buildMaintable() 'builds out the control
'to save space, I removed how this works. But, it creates a textbox that
'has a onKeyPress Event. When the user hits return, it calls the jQuery
'function serviceCall
Controls.Add(tblMainLayout)
Catch ex As Exception
Throw New ApplicationException("Exception Occurred", ex.InnerException)
End Try
End Sub
end Class
myScripts.js にある私の JQuery メソッド:
function serviceCall(getText, tbId, divId, bgColor) {
$.ajax({
type: "POST",
url: 'myClass.asmx/TestMethod',
data: '{"prefixText":"'some test'"}',
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function (msg) {
alert('Success')
},
error: function (e) {
alert('Error')
}
});
サービス クラスを作成し、それをユーザー コントロールと同じプロジェクトに含めましたが、サービスが呼び出されません。一方、Web プロジェクトの ASMX ファイルにサービスを配置すると、Web メソッドに問題なく到達します。
どんな助けでも素晴らしいでしょう
ありがとうジェイソン