Web service_c# tutorial on C # JSON return format

Source: Internet
Author: User
Tags wsdl
I paste C # 's code:
Copy Code code as follows:

Namespace iwebs.webs{
Using System;
Using System.Web.Services;
Using System.Web.Services.Protocols;
Using System.Xml;
Using System.Xml.XPath;
Using System.Web.Script.Services;
Using Iwebs;
[WebService (Name= "Cjjer", description= "a return user information, order information WebService, the requested cell phone number maximum 12", namespace= "http://www.cjjer.com/webs/")]
[System.Web.Script.Services.ScriptService]
public class cjjer:webservice{
public class reqheader:soapheader{
public string UserName;
public string Password;
}
public Reqheader header;
[WebMethod (Description = "Enter the int value ID of a single user, return the user class", messagename= "GetUser", EnableSession = False)]
[Scriptmethod (Responseformat = Responseformat.json)]
[SoapHeader ("header", Direction = soapheaderdirection.in)]
Public model.member getuser (int uid) {
This. Chechheader (header);
Return (new DAL. Members ()). GetByID (UID);
}
[WebMethod (Description = "Enter a user's mobile number, return the user class", messagename= "Getuserbymobile", EnableSession = False)]
[Scriptmethod (Responseformat=responseformat.json)]
[SoapHeader ("header", Direction = soapheaderdirection.in)]
Public Model.member Getuserbymobile (string umobile) {
This. Chechheader (header);
Return (new DAL. Members ()). Getbymobile (Umobile);
}
[WebMethod (Description = "Enter a user's mobile number, return order array", messagename= "Getordersbymobile", EnableSession = False)]
[Scriptmethod (Responseformat=responseformat.json)]
[SoapHeader ("header", Direction = soapheaderdirection.in)]
Public iwebs.model.order[] Getordersbymobile (string umobile) {
This. Chechheader (header);
Return (new DAL. Orders ()). Getbymobile (umobile,-365);
}
[WebMethod (Description = "Enter a user's ID, return order array", messagename= "Getordersbyuserid", EnableSession = False)]
[Scriptmethod (Responseformat=responseformat.json)]
[SoapHeader ("header", Direction = soapheaderdirection.in)]
Public iwebs.model.order[] Getordersbyuserid (int uid) {
This. Chechheader (header);
Return (new DAL. Orders ()). Getordersbyuserid (uid,-365);
}
private void Chechheader (Reqheader header) {
if (header!= null) {
if (header. MustUnderstand)
{
string UserName = Header.username;
string PassWord = Header.password;
if (UserName = = "Cjjer" && PassWord = = "000000")
{
return;
}
Else
{
throw new ApplicationException (String.Format ("username [{0}] or password [{1}] error", Username,password));
}
}
Else
{
throw new ApplicationException ("The message header containing the username and password information is not in the correct format");
}
}
Else
{
throw new ApplicationException ("Please submit a message header containing the username and password information");
}
}
};
}

Note that this request must be submitted to the SoapHeader, where the [System.Web.Script.Services.ScriptService]
This is the use of ajax.net processing JSON request, if not need to be free, if necessary, download ajax.net, and then put System.web.extensions.design.dll,system.web.extensions.dll in the bin, according to AJ Ax. NET default Web.config modify your Web.config and view it in the browser *. asmx file, if you use the WSDL to see the XML WSDL, the first step is successful.
One of the notes is:
Web.config
It is important to have two nodes in the HttpHandler:
Copy Code code as follows:

<remove verb= "*" path= "*.asmx"/>
<add verb= "*" path= "*.asmx" validate= "false" Type= "System.Web.Script.Services.ScriptHandlerFactory, System.Web.Extensions, version=3.5.0.0, culture=neutral, publickeytoken=31bf3856ad364e35 "/>

These two statements let ScriptHandlerFactory handle the webservice request.
Use AJAX requests when Http_request.setrequestheader ("Content-type", "Application/json");
Plus, the default return is JSON.
Attach the web. Config and the associated DLL file:
C # JSON
It makes sense when C # code is created.

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.