Use WCF to simplify Ajax code

Source: Internet
Author: User

The common Ajax program is HTML + ashx, which requires a lot of communication code. However, Microsoft's WCF integrates communication code and simplifies the code.

1. Create a New WCF Server Page, and create a method and data class for the client.

Using system; using system. LINQ; using system. runtime. serialization; using system. servicemodel; using system. servicemodel. activation; using system. servicemodel. web; namespace webapplication1 {[servicecontract (namespace = "")] [aspnetcompatibilityrequirements (requirementsmode = aspnetcompatibilityrequirementsmode. allowed)] public class wcfservice1 {// <summary> // obtain user information /// </Summary> /// <Param name = "ID"> User ID </param>/ // <returns> User class for storing User Information </returns> [operationcontract] public user getuser (int id) {return new user () {username = "Xiaojiang", age = 22, address = "Wenzhou, Zhejiang "};} // Add the [webget] attribute to use http get [operationcontract] public void dowork () {// Add the operation here to implement return ;} // Add more operations here and use [operationcontract] to mark them} // <summary> // define the user class, store user data // </Summary> public class user {Public String username {Get; set;} public int age {Get; set;} Public String address {Get; set ;}}}

 

2. Add An ASPX page, add a scriptmanager control at the top of the page, and add the server page in the data set of the control,

Finally, it is very convenient to write JS Code just like writing C # programs.

<% @ Page Language = "C #" autoeventwireup = "true" codebehind = "wcfajax. aspx. cs" inherits = "webapplication1.wcfajax" %> <! Doctype HTML public "-// W3C // dtd xhtml 1.0 transitional // en" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <HTML xmlns = "http://www.w3.org/1999/xhtml"> 

 

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.