2013-10-09 59 views
0

我在我的項目中編寫了一個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'失敗。」

請幫助我。

回答

2

因爲您使用的是v6.x,您可以使用Umbraco WebAPI實現嗎?因此,而不是創建一個web服務,按照這篇文章創建一個WebAPI控制器/操作:http://our.umbraco.org/documentation/Reference/WebApi/

這樣做的好處是,您可以直接從控制器訪問所有的Umbraco上下文和服務。

顯然這意味着你不一定會得到webmethod的好處,但是如果你返回json並將它反序列化爲一個你可以使用的強類型,你仍然可以在你的工作中做很多工作。

+0

我用過ASP.NET Web Form。我認爲使用WebAPI是不可能的。 – Jahan

+0

就是這樣。如果您使用的是v6,則可以在同一個項目中實際使用masterpages和mvc以及web api。我經常使用WebAPI來設置API,專門用於從js文件調用以提供JSON數據,而不管js是由webform還是mvc視圖加載。 – Digbyswift

0

WebMethods不能在usercontrols中。我建議使用webservice(.asmx)並將webmethod放置在那裏。

+0

我已經使用ApplicationFormService.asmx但沒有工作。 – Jahan

0

我不認爲你在Umbraco寫了一個Web服務 - 聽起來你正試圖與Umbraco共同定位一個。爲什麼不將服務引用添加到現有的Web服務?或者,在您的Umbraco解決方案中包含WebApi庫並進行Restful調用。