ASP. NET call Web Services Method

Source: Internet
Author: User
Tags xsl

In Atlas, its "Web Services" are placed in a special running environment and executed to ASP in some cases. NET original component execution, which has been analyzed in the previous article). Therefore, even if we do not access it through AJAX, we only need to understand the behavior of Atlas's special runtime environment, it still brings us some other usage methods. The following example illustrates how to use http get to call the Web Services method on the Atlas server, unless otherwise specified, all of the following explanations are for Atlas extensions, rather than ASP.. NET ).

First, we will write a Web Serivces method:

 
 
  1. [WebMethod]  
  2. [WebOperation(true, ResponseFormatMode.Xml)]  
  3. public XmlDocument Vote(string name, int id)  
  4. {  
  5. XmlDocument responseDoc = new XmlDocument();  
  6. responseDoc.LoadXml(  
  7.  "<?xml-stylesheet type=\"text/xsl\" href=\"Vote.xsl\"?>" +  
  8.  "<response><user></user><id></id></response>");  
  9. responseDoc.SelectSingleNode("//user").InnerText = name;  
  10. responseDoc.SelectSingleNode("//id").InnerText = id.ToString();  
  11. return responseDoc;  
  12. }  
  13.  

In Atlas, http post is the default support method for Web Services, which is also an inevitable support method. If you need to enable the Web Service method to Support http get, you must use Microsoft. Web. Services. WebOperationAttribute to label it like the code above. The first parameter of WebOperationAttribute is getVerbEnabled. true indicates that the http get method is supported. The second parameter Microsoft. Web. Services. ResponseFormatMode. Xml indicates that the output method of the result object is XML, rather than the default JSON.

Here, we use XML because JSON has no meaning here. After returning JSON, it is used to obtain the content and run the Javascript function eval to obtain the object in JSON format. Here, we aim to display the results to users. Therefore, we can return results in XML format. With the help of XSL, We can display the results to users in HTML format.

Then there is a simple XSL:

 
 
  1. <?xml version="1.0" encoding="utf-8"?> 
  2. <xsl:stylesheet version="1.0" 
  3. xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> 
  4. <xsl:template match="/response"> 
  5. <html> 
  6. <head> 
  7. <title>Thanks for your participation.</title> 
  8. </head> 
  9. <body style="font-family:Verdana; font-size:13px;"> 
  10. <h4>Thanks for your participation.</h4> 
  11. <div> 
  12. <xsl:text>Dear </xsl:text> 
  13. <xsl:value-of select="user"/> 
  14. <xsl:text>, you've voted for item </xsl:text> 
  15. <xsl:value-of select="id"/> 
  16. <xsl:text>.</xsl:text> 
  17. </div> 
  18. </body> 
  19. </html> 
  20. </xsl:template> 
  21. </xsl:stylesheet>  

Next is our HTML file. Our goal is to get the user input information, splice it into a URL, and open it in a new window. Therefore, we do not need to use Atlas here. The Code is as follows:

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

After you click the "Vote" button, the Javascript function Vote () is called (). The Code is as follows:

 
 
  1. <script language="javascript"> 
  2. function vote()  
  3. {  
  4. var url = "HttpGetWebService.asmx?mn=Vote";  
  5. url += ("&name=" + encodeURI(document.getElementById("txtName").value));  
  6. url += ("&id=" + document.getElementById("comboItem").value);  
  7.  
  8. window.open(url);  
  9. }  
  10. </script> 

The URL to be spliced is simple: first set mn IN QueryString to the name of the Web Services method to be called, and then the parameters required to call the Web Services method in QueryString. Note that since URL splicing is used, you must use encodeURI for encoding before using it. Otherwise, exceptions may occur. The preceding section describes how to use ASP. NET to call Web Services.

  1. ASP. net mvc Web Application Project
  2. IIS6 ASP. net isapi request processing process
  3. Seven user management controls for ASP. NET controls
  4. RSA encryption for ASP. Net
  5. How to obtain database strings using ASP. NET

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.