代码之家  ›  专栏  ›  技术社区  ›  x19

如何在Umbraco中编写web服务?

  •  0
  • x19  · 技术社区  · 12 年前

    我在我的项目中为提交表单写了一个web服务,但它不起作用。我使用过Umbraco CMS 6.1.5。

    描述:

    我在里面写信 大师大师 :

    <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>
    

    我在里面写信 应用程序窗体服务.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";
        }
    }
    

    我在里面写信 应用程序窗体控制.ascx.cs :

    [WebMethod]
        [System.Web.Script.Services.ScriptMethod(UseHttpGet = true)]
        public static void HelloWorld()
        {
            ApplicationFormService s = new ApplicationFormService();
            s.HelloWorld();
        }
    

    我在里面写信 默认.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>
    

    我在里面写信 建筑.js :

    function btnSendApplicationForm_onclick() {
         PageMethods.HelloWorld(_onMethodComplete, _onMethodError);
    }
    
    function _onMethodComplete(result) {
        alert(result.message);
    }
    
    function _onMethodError(result) {
        alert(result._message);
    }
    

    当代码运行PageMethods.HelloWorld(_onMethodComplete,_onMethod error)时,我收到了一个错误表单_onMethode error,它告诉我

    “服务器方法“HelloWorld”失败。”

    请帮帮我。

    3 回复  |  直到 12 年前
        1
  •  2
  •   Digbyswift    12 年前

    既然你使用的是v6.x,你能使用Umbraco WebAPI实现吗?因此,与其创建Web服务,不如根据本文创建一个WebAPI控制器/操作: http://our.umbraco.org/documentation/Reference/WebApi/

    这样做的好处是,您可以直接从控制器访问所有Umbraco上下文和服务。

    显然,这意味着您不一定会从 webmethod 但是,如果您返回json并将其反序列化为可以使用的强类型,那么您仍然可以在中完成大部分工作。

        2
  •  0
  •   Daniel Bardi    12 年前

    WebMethods不能在用户控件中。我建议使用Web服务(.asmx),并将您的Web方法放在那里。

        3
  •  0
  •   MikeW    12 年前

    我不认为你在Umbraco写一个网络服务,听起来你想和Umbraco一起定位一个。为什么不将服务引用添加到现有的web服务?或者,在您的Umbraco解决方案中包含WebApi-libs,并进行Restful调用。