温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

ASP.NET中怎么调用Web Services方法

发布时间:2021-06-24 16:21:16 来源:亿速云 阅读:144 作者:Leah 栏目:编程语言

ASP.NET中怎么调用Web Services方法,相信很多没有经验的人对此束手无策,为此本文总结了问题出现的原因和解决方法,通过这篇文章希望你能解决这个问题。

首先,我们写一个Web Serivces方法:

[WebMethod]  [WebOperation(true, ResponseFormatMode.Xml)]  public XmlDocument Vote(string name, int id)  {  XmlDocument responseDoc = new XmlDocument();  responseDoc.LoadXml(   "<?xml-stylesheet type=\"text/xsl\" href=\"Vote.xsl\"?>" +   "<response><user></user><id></id></response>");  responseDoc.SelectSingleNode("//user").InnerText = name;  responseDoc.SelectSingleNode("//id").InnerText = id.ToString();  return responseDoc;  }

在Atlas中,HTTP POST为Web Services的默认支持方法,也是必然的支持方法。而如果需要使该Web Service方法支持HTTP GET的话,就必须如上面代码一样,使用Microsoft.Web.Services.WebOperationAttribute进行标注。 WebOperationAttribute的***个参数就是getVerbEnabled,true则表示支持HTTP GET方法。第二个参数Microsoft.Web.Services.ResponseFormatMode.Xml则表示结果对象的输出方式为 XML,而不是默认的JSON。

在这里,我们使用XML的原因是因为JSON在这里没有任何意义。返回JSON后是为了在获得这些内容之后通过Javascript函数eval执行,从而获得JSON表示的对象。而在这里,我们的目的是将结果显示给用户看,所以使用XML形式返回,再加上XSL的支持,就能以HTML的形式显示给用户了。

然后就是简单的XSL:

<?xml version="1.0" encoding="utf-8"?> <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> <xsl:template match="/response"> <html> <head> <title>Thanks for your participation.</title> </head> <body style="font-family:Verdana; font-size:13px;"> <h5>Thanks for your participation.</h5> <div> <xsl:text>Dear </xsl:text> <xsl:value-of select="user"/> <xsl:text>, you've voted for item </xsl:text> <xsl:value-of select="id"/> <xsl:text>.</xsl:text> </div> </body> </html> </xsl:template> </xsl:stylesheet>

接下来就是我们的HTML文件。我们的目的非常简单,就是得到用户输入的信息,拼接成URL之后在新窗口中打开。因此我们在这里根本无需使用Atlas。代码如下:

<div>Name:<input type="text" id="txtName" /></div> <div>Item:  <select id="comboItem"> <option value="1">Item 1</option> <option value="2">Item 2</option> <option value="3">Item 3</option> <option value="4">Item 4</option> <option value="5">Item 5</option> </select> </div> <input type="button" value="Vote" onclick="vote()" />

点击“Vote”按钮后,就会调用Javascript函数Vote()。代码如下:

<script language="javascript"> function vote()  {  var url = "HttpGetWebService.asmx?mn=Vote";  url += ("&name=" + encodeURI(document.getElementById("txtName").value));  url += ("&id=" + document.getElementById("comboItem").value);   window.open(url);  }  </script>

看完上述内容,你们掌握ASP.NET中怎么调用Web Services方法的方法了吗?如果还想学到更多技能或想了解更多相关内容,欢迎关注亿速云行业资讯频道,感谢各位的阅读!

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI