Use ASP. NET AJAX to access WebService asynchronously in ASP.

Source: Internet
Author: User

Friends who have used ASP. NET AJAX know how to access WebService through ASP. NET AJAX, but there's no big difference in using ASP. NET AJAX Async Access WebService in ASP.

The use of ASP. NET Ajax access in an ASP. WebService is usually a way to introduce WebService to generate client proxies through Scriptmananger, and you can also use Microsoft Ajax Library to complete. This article describes the use of ASP. NET Ajax in ASP. WebService (ScriptManager and Microsoft Ajax Library two ways).

1. Preparatory work
Create a new ASP. NET MVC application that will automatically introduce the Microsoft Ajax Library when you create a new MVC application in Microsoft ASP CodePlex Preview 5, as follows:

Create an Ajax directory under the views file directory in the new ASP. NET MVC Application project above and add a new ASP. NET MVC page ajaxpage.aspx. For example:

We go to ajaxpage.aspx code file will find that the page class from the original inherited page class changed to ViewPage, as follows:

<!--<br/><br/>code highlighting produced by Actipro Codehighlighter (freeware) <br/>http://www. Codehighlighter.com/<br/><br/>-->1namespacemvcandajax.views.ajax
2{
3publicpartialclassajaxpage:viewpage
4{
5}
6}

OK, at this time our ajaxpage.aspx is not running, to let it run normally we have to help it, in the controllers file under the new controller, named: Ajaxcontroller. Define an action method on the inside for Ajaxpage.aspx Ajaxpage ():

<!--<br/><br/>code highlighting produced by Actipro Codehighlighter (freeware) <br/>http://www. Codehighlighter.com/<br/><br/>-->1namespacemvcandajax.controllers
2{
3publicclassajaxcontroller:controller
4{
5publicActionResultAjaxPage ()
6{
7returnView ();
8}
9}
10}

Now we can access the MVC page on the browser via: Http://localhost:2048/Ajax/AjaxPage. (This address is only native debugging)

2. Use ScriptManager to asynchronously access WebService on the client in ASP.
OK, the above preparations are complete, let's take a look at how the client accesses Webserivice through ScriptManager.

First develop the WebService, create a new WebService (Ajaxservice.asmx), and provide the method to the client to invoke. The following code:

<!--<br/><br/>code highlighting produced by Actipro Codehighlighter (freeware) <br/>http://www. Codehighlighter.com/<br/><br/>-->1namespacemvcandajax.services
2{
3[webservice (namespace= "http://tempuri.org/")]
4[webservicebinding (conformsto=wsiprofiles.basicprofile1_1)]
5[toolboxitem (false)]
6[system.web.script.services.scriptservice]
7publicclassajaxservice:system.web.services.webservice
8{
9
10[webmethod]
11publicUsersGetUser ()
12{
13returnnewUsers
14{
15id=1,
16name= "Zhangsan",
17password= "123456"
18};
19}
20
21[webmethod]
22publicUsersSetUser (INTID,STRINGNAME,STRINGPWD)
039
24returnnewUsers
059
26id=id,
27name=name,
28password=pwd
29};
30}
31}
32}

This webservice uses the custom users object, as defined by the Users object:

<!--<br/><br/>code highlighting produced by Actipro Codehighlighter (freeware) <br/>http://www. Codehighlighter.com/<br/><br/>-->1namespacemvcandajax
2{
3publicclassUsers
4{
5publicintid{get;set;}
6publicstringname{get;set;}
7publicstringpassword{get;set;}
8}
9}

Like the traditional ASP. NET, we need to add the ScriptManager control to the ASPX page, and the control's Services property introduces the webservice that needs to be accessed to this page to generate the client proxy.

<!--<br/><br/>code highlighting produced by Actipro Codehighlighter (freeware) <br/>http://www. codehighlighter.com/<br/><br/>-->1<asp:scriptmanagerid= "ScriptManager1" runat= "Server" >
2<services>
3<asp:servicereferencepath= "~/services/ajaxservice.asmx"/>
4</services>
5</asp:scriptmanager>

WebService provides two methods that allow the client to invoke GetUser (), SetUser (), and the previous one directly returns a users object, which is the data (JSON string) that is passed on the page. Constructs a users object to return to the calling client. Front-End Call page design:

Page HTML code

The user-side invocation is also the same as in traditional ASP. NET call, through the [namespace. Class name. Method Name (parameter list ...)] This method calls WebService.

<!--<br/><br/>code highlighting produced by Actipro Codehighlighter (freeware) <br/>http://www. Codehighlighter.com/<br/><br/>-->1<scriptlanguage= "javascript" type= "Text/javascript" >
2functionbtnrequest_onclick ()
3{
4mvcandajax.services.ajaxservice.getuser (Success);
5}
6
7functionbtnRequestClick ()
8{
9varjsonuser={
"ID": $get ("txtID"). Value,
"Name": $get ("Txtname"). Value,
"Passwrod": $get ("Txtpassword"). Value
13};
14mvcandajax.services.ajaxservice.setuser (jsonuser,success);
15}
16
17functionsuccess (Result)
18{
19varuser=string.format ("Id:{0}<br/>name:{1}<br/>password:{1}", Result.id,result. Name,result. Password);
20$get ("Result"). Innerhtml=user;
21}
22</script>

As with the traditional ASP. NET, there is no detailed introduction, this article will provide a sample program download, you can view the sample program directly.

3. Asynchronously access WebService on the client through the Microsoft Ajax Library
Using the library is very simple, before using ScriptManager, to use the library is actually using it instead of ScriptManager to complete the introduction of WebService to the client to generate the client agent. OK, Below we remove the ScriptManager on the page and introduce the Microsoft Ajax Library to the

<!--<br/><br/>code highlighting produced by Actipro Codehighlighter (freeware) <br/>http://www. codehighlighter.com/<br/><br/>-->1<scripttype= "Text/javascript" src= "http://www.cnblogs.com/ Content/microsoftajax.js "></script>
2<scripttype= "Text/javascript" src= "/services/ajaxservice.asmx/js" ></script>


We only need to introduce the Microsoft Ajax Library as above and then generate the WebService Client agent, and now run the program as above with ScriptManager introduced WebService to build the client agent.

This article describes here, detailed downloadable sample programs to view.

Sample program Download: Click to download

Use ASP. NET AJAX to access WebService asynchronously in ASP.

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.