Asp. Net WebServices, asp. netwebservices
Recently, I have read some documents about webServices and made an example to sort them out, mainly including creating services, background access, and front-end Ajax access on the. net platform.
1. Create a webServices service.
1. Open vs (My 2012) and create a project-> Web-> ASP. NET empty Web empty application.
2. Right-click the project and choose "Add new project"> "Web"> "Web service.
3. Add two methods:
1> obtain uppercase text based on Arabic numerals
2> get the user list
The Code is the same as the General C # code. The overall code is as follows:
1 using System. Collections. Generic; 2 using System. Web. Services; 3 4 namespace WebServices 5 {6 [WebService (Namespace =" http://tempuri.org/ ")] 7 [WebServiceBinding (ConformsTo = WsiProfiles. basicProfile1_1)] 8 [System. componentModel. toolboxItem (false)] 9 // to allow ASP.. net ajax calls this Web service from a script. uncomment the following lines. 10 [System. web. script. services. scriptService] 11 public class GetBigString: webService12 {13 /// <summary> 14 /// the request is capitalized Based on Arabic numerals or 15 /// </summary> 16 /// <param name = "number"> </param> 17 // <returns> </returns> 18 [WebMethod] 19 public string ConvertBit (int number) 20 {21 string str; 22 switch (number) 23 {24 case str = "1"; 26 break; 27 case str = "2"; 29 break; 30 case :31 str = "3"; 32 break; 33 default: 34 str = "enter the correct number"; 35 break; 36} 37 return str; 38} 39 40 // <summary> 41 // obtain the student collection 42 // </summary> 43 // <param name = "stringId"> </param> 44 // <returns> </returns> 45 [WebMethod] 46 public List <Studnet> GetStudentList (string stringId) 47 {48 var list = new List <Studnet> (); 49 var t = new Studnet {Address = "ShuShui town, Xianning City, Tongcheng County, Hubei Province", Age = 10, name = "Zhang San"}; 50 var t2 = new Studnet {Address = "Haidian District, Beijing", Age = 10, Name = "Li Si"}; 51 list. add (t); 52 list. add (t2); 53 return list; 54} 55} 56 57 public class Studnet58 {59 public string Name {get; set;} 60 61 public int Age {get; set ;} 62 63 public string Address {get; set;} 64 65} 66}View Code
Set this asmx file as a startup Item. When you run the project, you can see the description file of this service, for example, "the following operations are supported. For formal definitions, see "service description. Click "?" in the address bar. Or click "service description" to view the instructions for this webservices, such as http: // localhost: 4828/GetBigString. asmx? WSDL. In short, the page shows that the service is successfully started. You can deploy the program to IIS to facilitate client calls.
2. Calling in a program by referencing a service
1. Create another web project.
2. Right-click "Reference" and choose "add service reference...".
3. Enter the service address in the browser and click "go to". (If there is no deployed IIS, you must run the service project when adding a reference. Otherwise, you cannot add a reference)
4. Enter the custom namespace and click OK. The "Service References" folder appears in the project.
5. Call the service in the project by instantiating the class.
I added An aspx file to my web project and added the following code to the backend cs code:
1 using System; 2 using System. web. UI; 3 using WebServices; 4 5 namespace Client 6 {7 public partial class Index: Page 8 {9 protected void Page_Load (object sender, EventArgs e) 10 {11 // call WebServices, assign the result to the page control 12 TextBox1.Text = GetNumberString (1); 13} 14 15 private string GetNumberString (int numner) 16 {17 // instantiate the service class 18 var g = new GetBigString (); 19 var str = g. convertBit (numner); 20 return str; 21} 22} 23}View Code
The page code is as follows:
1 <body> 2 <form id = "form1" runat = "server"> 3 <div> 4 <asp: textBox ID = "TextBox1" runat = "server"> </asp: TextBox> 5 </div> 6 </form> 7 </body>View Code
Run the web Project and the corresponding results are displayed on the page. Show: "1"
Idea: when running a web client project, the WebServices project needs to be started.
3. Use ajax to call WebServices
Or the preceding WebServices service, which is called through front-end ajax. The js Code is as follows:
1 <script type = "text/javascript"> 2 $ (document ). ready (function () {3 $. ajax ({4 type: "post", // access WebService using Post to request 5 url: "http: // localhost: 8066/GetBigString. asmx/GetStudentList "// WebService address and method name Combination 6 data:" {stringId: '1'} ", // Email parameter 7 contentType:" Application/Json ", // sending information content encoding type 8 success: function (data) {9 console. log (data. d); 10} 11}); 12}); 13 </script>View Code
You can output the json data corresponding to the list data in WebServices on the console.
4. What are the points to consider:
1. During the process of adding a service reference or calling through ajax, the WebServices project is running.
2. In the GetStudentList method, we define a string-type parameter. Although it is not used in the project, parameters must be passed during ajax call; otherwise, an error is returned.
3. When passing a parameter, you must enclose the entire parameter in single quotes.
4. If ajax is required, you need to cancel the first line of comments of the WebServices file:
// To allow ASP. net ajax to call this Web service from a script, uncomment the following line.
[System. Web. Script. Services. ScriptService]
5. Cross-origin ajax requests may occur during ajax calls.
Blocked cross-source requests: The same-source policy prohibits reading remote resources located in. (Cause: the CORS header lacks 'access-Control-Allow-origin '). Solution reference: solve cross-origin problem of ajax request cors
By the end of this instance, the technology is limited, and the simple real column is for reference only.
Download source code dome