. NET implementation WebService Simple instance

Source: Internet
Author: User

Principle: WebService is an SOA (Service Oriented Programming) architecture, it is not dependent on the language, does not rely on the platform, can be implemented in different languages of mutual invocation, through the Internet for HTTP protocol-based network applications between the interaction.

Function: Mainly used for data exchange, providing service interface.
Advantages: Easy to cross-platform, easy to deploy, and no additional client support.

First, create a WebService service

1. Create an empty ASP. NET Web application.

2. In the Web program right-click Add-New Item--web service. Steps

  

3. Write the WebService code, open your new WebService service, and you will see the following code:

  

usingSystem;usingSystem.Collections.Generic;usingSystem.Linq;usingsystem.web;usingSystem.Web.Services;namespacewebservicedemo{/// <summary>    ///Summary description of Testservice/// </summary>[WebService (Namespace ="http://tempuri.org/")] [WebServiceBinding (ConformsTo=Wsiprofiles.basicprofile1_1)] [System.ComponentModel.ToolboxItem (false)]    //to allow the Web service to be called from the script using ASP. NET AJAX, uncomment the downstream. //[System.Web.Script.Services.ScriptService]     Public classTestService:System.Web.Services.WebService {[WebMethod] Public stringHelloWorld () {return "Hello World"; }        //methods of extending[WebMethod] Public stringPrint (stringinfo) {            returninfo; }    }}
View Code

4. Compile and run. The ability to see such a picture shows that WebService was created successfully.

  

Second, the background of the call mode.

1. Click on the project reference, right button, select Add Service app.

2. Enter the URL address of the webservice in the address bar, then modify the namespace, and finally click Go.

  

3. Background calls. Create a new paging file and test it with the following code in the Page_Load method.

  

usingSystem;usingSystem.Collections.Generic;usingSystem.Linq;usingsystem.web;usingSystem.Web.UI;usingSystem.Web.UI.WebControls;namespacewebservicedemo{ Public Partial classTestWebService:System.Web.UI.Page {protected voidPage_Load (Objectsender, EventArgs e) {Registerhyperlink.navigateurl="register.aspx? Returnurl="+ Httputility.urlencode (request.querystring["RETURNURL"]); //Calling MethodsTestservicereference.testservicesoapclient Testservice =Newtestservicereference.testservicesoapclient (); stringinfo = Testservice.write ("HelloWorld");        Page.Response.Write (info); }    }}
View Code

If you can see the information, test OK.

Third, the front desk JS call. Refer to JS first.

1. Modify the Testservice.asmx code.

  

usingSystem;usingSystem.Collections.Generic;usingSystem.Linq;usingsystem.web;usingSystem.Web.Services;namespacewebservicedemo{/// <summary>    ///Summary description of Testservice/// </summary>[WebService (Namespace ="http://tempuri.org/")] [WebServiceBinding (ConformsTo=Wsiprofiles.basicprofile1_1)] [System.ComponentModel.ToolboxItem (false)]    //to allow the Web service to be called from the script using ASP. NET AJAX, uncomment the downstream. [System.Web.Script.Services.ScriptService] Public classTestService:System.Web.Services.WebService {[WebMethod] Public stringGetweekname (intDay ) {            stringresult; if(Day >6) {result="Incorrect input format"; }            Else{DayOfWeek Week=(DayOfWeek) Day; Result=Week.            ToString (); }            returnresult; }    }}
View Code

2.JS Code (jquery code to be introduced)

  

<script type="Text/javascript">$ (function () {$.ajax ({type:'POST', URL:'Testservice.asmx/getweekname', Data:'{day:1}', DataType:'JSON', ContentType:"Application/json", Success:function (data) {alert (DATA.D);        }            });    }); </script>
View Code

3. Run the test.

  

Test OK, a simple webservice is implemented.

Summary: There is time some things are not as difficult as you think, as long as you go to understand, understand its principles, the implementation of code is quite simple.

  

  

  

. NET implementation WebService Simple instance

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.