Presentation is loading. Please wait.

Presentation is loading. Please wait.

Internet Technologies and Web Application Web Services With ASP.NET Tutorial: Introduction to.

Similar presentations


Presentation on theme: "Internet Technologies and Web Application Web Services With ASP.NET Tutorial: Introduction to."— Presentation transcript:

1 Internet Technologies and Web Application Web Services With ASP.NET Tutorial: Introduction to

2 Lesson 1: Creating a Web Service

3 Lesson 1 – Creating a Web Service  Exercise 1.1 – Create and understand a web service  Web Service is objects and methods that can be invoked from any client over HTTP.  In this exercise, you will create a simple Web service with “Hello World” method using Visual Studio.NET.  You will test the Web service through its associated documentation web page.

4 1. Launch Microsoft Visual Studio.NET 2008. 2. Create new website, choose Visual C#, select location as HTTP and enter site location as http://localhost/itcs426/myService, then click OK. Lesson 1 – Creating a Web Service

5 3. Create new web service, click WebSite ->Add New Item… 4. Select Web Service icon and enter file name as WebService.asmx, then click Add button. 5. The default page of web service class will be created. Lesson 1 – Creating a Web Service

6 1) ASP.NET web service class - kept in App_Code Folder only, used to create web methods 2) “WebService.asmx” file – let the client call web methods 3) “HelloWorld” method – default method in web service class 1 1 2 2 3 3 Lesson 1 – Creating a Web Service

7 6. Test the web service, right click on WebService.asmx in solution explorer, then click Set As Start Page, press F5 to run the solution (start debugging). 7. Internet Explorer will start and display a documentation page for the web service as seen in the following illustration. Lesson 1 – Creating a Web Service

8 8. Click “HelloWorld” method to test the operation using the HTTP POST protocol, then click the Invoke button. 9. You will see a resulting XML page that contains the result of the “HelloWorld” method as seen in the following illustration. Lesson 1 – Creating a Web Service

9 Lesson 2: Passing Parameters and Returning Values in Web Service

10  Exercise 2.1 – Create a web method with parameter passing and returning values  In this exercise, you will create a new web method “HelloUser” which will get the parameter passed from web page and return back the string value.  You will test the Web service through its associated documentation web page. Lesson 2 – Passing Parameters and Returning Values in Web Service

11 1. Create new web method “ HelloUser ”. [WebMethod] public string HelloUser(string yourName) { return "Hello,"+ yourName; } 2. Press F5 to run the solution. 3. Test the Web service method in browser. To test the method, click on the link for the method name “ HelloUser ”, type your name in the text box and then click Invoke button to view the result. Lesson 2 – Passing Parameters and Returning Values in Web Service

12

13 Lesson 3: Adding a Web Reference

14  Exercise 3.1 – Add a web reference to the web service  Web reference is proxy class created on the client to connect to the web service running on the server.  In this exercise, you will learn how to add a web reference to the web service. Lesson 3 – Adding a Web Reference

15 1. Click WebSite->Add web Reference.. 2. On Add Web Reference window  Enter URL Web Service: http://HostName/itcs426/myService/ WebService.asmx  Host Name: IP Address or domain Web Service  Enter your Web Reference name: WebReference  Click Add Reference button Lesson 3 – Adding a Web Reference

16

17 3. Result in solution explorer. Lesson 3 – Adding a Web Reference

18 Lesson 4: Calling a Web Service via Web Application

19  Exercise 4.1 – Call a web service via web application  In this exercise, you will create an ASP.NET web page that uses the web service you created in lesson 2.  You will test the web service client that will pass the parameter to the web service. Lesson 4 – Calling a Web Service via Web Application

20 1. Open “ Default.aspx ” page, then add textbox, button, and label in the design view.  Textbox ID: yourName  Button ID: callService  Label ID: valueReturn Lesson 4 – Calling a Web Service via Web Application

21 2. Create method “ callService_Click” for “callService ” button. protected void callService_Click(object sender, EventArgs e) { //Create new object of web reference for call web servic e WebReference.WebService callResult = new WebReference.WebService(); //Call method HelloUser and pass parameter to method //Method HelloUser returns result to label control valueReturn.Text = callResult.HelloUser(yourName.Text); } Lesson 4 – Calling a Web Service via Web Application

22 3. Press F5 to run the solution. 4. Test the application in browser. Enter your name in textbox and click Result button. Lesson 4 – Calling a Web Service via Web Application

23 Lesson 5: Modifying a Web Service

24  Exercise 5.1 – Modify web service by adding more web methods  In this exercise, you will add 2 web methods to your existing web service class.  “getQuestionList” method will return the arrayList for binding to dropDownList.  “checkAnswer” method will get the selected index from dropDownList as a parameter and return the answer in string format. Lesson 5 – Modifying a Web Service

25 1. Create new web methods “ getQuestionList ” and “ checkAnswer ” in WebService class. [WebMethod] public ArrayList getQuestionList() { ArrayList list = new ArrayList(); list.Add("Select Question"); list.Add("What is your name?"); list.Add("What is your student ID?"); return list; } Lesson 5 – Modifying a Web Service

26 [WebMethod] public string checkAnswer(int questionIndex) { string answer; switch (questionIndex) { case 0: answer = "Select Question"; break; case 1: answer = "My name is Enter Name "; break; Lesson 5 – Modifying a Web Service

27 case 2: answer = "My Student ID : Student ID Number "; break; default: answer = "Select Question"; break; } return answer; } Lesson 5 – Modifying a Web Service

28 2. In solution explorer, update web reference by right clicking at App_WebReferences folder and click Update Web\Service. 3. Add dropDownList and Label to web page (Default.aspx) in design view.  dropDownList ID: List Question  dropDownList AutoPostBack: True  Label ID: AnswerValue Lesson 5 – Modifying a Web Service

29 3. Add code in code-behind (default.aspx.cs) protected void Page_Load(object sender, EventArgs e) { if (!IsPostBack) { WebReference.WebService getDataSource; getDataSource = new WebReference.WebService(); ListQuestion.DataSource = getDataSource.getQuestionList(); ListQuestion.DataBind(); } Lesson 5 – Modifying a Web Service

30 protected void ListQuestion_SelectedIndexChanged(object sender, EventArgs e) { WebReference.WebService getAnswer; getAnswer = new WebReference.WebService(); AnswerValue.Text = getAnswer.checkAnswer(ListQuestion.SelectedIndex); } Lesson 5 – Modifying a Web Service

31 4. Run and test the result in browser. Lesson 5 – Modifying a Web Service

32 Lesson 6: Web Services in Real Application

33  Exercise 6.1 – Call a web service from another machine and display in gridView.  In this exercise, you will learn to call web service from server and display the result in gridView in the form of table. Lesson 6 – Web Service in Real Application

34 1. Add web reference  URL: http://10.22.6.132/CustomerService/Customer.as mx  Web reference name: CustomerUpdate 2. Create new ASP.NET web form “ Customer.aspx ” in design page. Add textbox, button, and gridView.  Textbox ID: customerName  Button ID: saveCustomer  GridView ID: viewCustomer  GridView Set AutoFormat: Classic Lesson 6 – Web Service in Real Application

35

36 2. Create new ASP.NET web form “ Customer.aspx ” in design page. Add textbox, button, and gridView.  Textbox ID: customerName  Button ID: saveCustomer  GridView ID: viewCustomer  GridView Set AutoFormat: Classic Lesson 6 – Web Service in Real Application

37

38 3. Add code in code-behind (default.aspx.cs) protected void loadCustomer() { CustomerUpdate.Service cus = new CustomerUpdate.Service(); viewCustomer.DataSource = cus.viewCustomer(); viewCustomer.DataBind(); } protected void Page_Load(object sender, EventArgs e) { if (!IsPostBack){ loadCustomer(); } Lesson 6 – Web Service in Real Application

39 protected void saveCustomer_Click(object sender, EventArgs e) { CustomerUpdate.Service cus = new CustomerUpdate.Service(); int result = cus.updateCustomer(CustomerName.Text, "Your Student ID"); if (result == 1) { loadCustomer(); } else { Response.Write("Insert faile !"); } Lesson 6 – Web Service in Real Application

40 3. Run and test in browser Lesson 6 – Web Service in Real Application

41 Internet Technologies and Web Application


Download ppt "Internet Technologies and Web Application Web Services With ASP.NET Tutorial: Introduction to."

Similar presentations


Ads by Google