我在我的項目中編寫了一個Web服務來提交表單,但它不起作用。我用過Umbraco CMS 6.1.5。如何在Umbraco中編寫Web服務?
說明:
我Master.master寫道:
<form id="AbniyehMainForm" runat="server">
<asp:ScriptManager ID="ScriptManager1" runat="server" EnablePartialRendering="true"
EnablePageMethods="true" ScriptMode="Auto">
<Services>
<asp:ServiceReference Path="~/Services/ApplicationFormService.asmx" />
</Services>
<Scripts>
<asp:ScriptReference Path="/scripts/building.js" />
</Scripts>
</asp:ScriptManager>
</form>
我ApplicationFormService.asmx寫道:
[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
[System.ComponentModel.ToolboxItem(false)]
// To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line.
[System.Web.Script.Services.ScriptService]
public class ApplicationFormService : System.Web.Services.WebService
{
[WebMethod]
[ScriptMethod(UseHttpGet = true)]
public string HelloWorld()
{
return "Hello World";
}
}
我ApplicationFormControl.ascx寫道。 cs:
[WebMethod]
[System.Web.Script.Services.ScriptMethod(UseHttpGet = true)]
public static void HelloWorld()
{
ApplicationFormService s = new ApplicationFormService();
s.HelloWorld();
}
我的Default.aspx寫道:
<%@ Import Namespace="System.Web.Services" %>
<%@ Import Namespace="Defraz.Building.WebApp.Services" %>
<script runat="server" type="text/C#" language="c#">
[WebMethod]
[System.Web.Script.Services.ScriptMethod()]
public static string HelloWorld()
{
return "HelloWorld!!!";
}
</script>
我寫道Building.js:
function btnSendApplicationForm_onclick() {
PageMethods.HelloWorld(_onMethodComplete, _onMethodError);
}
function _onMethodComplete(result) {
alert(result.message);
}
function _onMethodError(result) {
alert(result._message);
}
當代碼在運行PageMethods.HelloWorld(_onMethodComplete ,_onMethodError),我收到一個錯誤表單_onMethodError,告訴我
「服務器方法'HelloWorld'失敗。」
請幫助我。
我用過ASP.NET Web Form。我認爲使用WebAPI是不可能的。 – Jahan
就是這樣。如果您使用的是v6,則可以在同一個項目中實際使用masterpages和mvc以及web api。我經常使用WebAPI來設置API,專門用於從js文件調用以提供JSON數據,而不管js是由webform還是mvc視圖加載。 – Digbyswift