当前位置: 编程技术>.net/c#/asp.net
c# JSON返回格式的WEB SERVICE
来源: 互联网 发布时间:2014-10-13
本文导语: 我贴c#的代码: 代码如下: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="一个返回用户资料,订单...
我贴c#的代码:
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="一个返回用户资料,订单信息的WebService,请求的手机号码最长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 ="输入单个用户的int值ID,返回用户类",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 ="输入某个用户的手机号码,返回用户类",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 ="输入某个用户的手机号码,返回订单数组",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 ="输入某个用户的ID,返回订单数组",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("用户名[{0}]或密码[{1}]错误",UserName,PassWord));
}
}
else
{
throw new ApplicationException ("包含用户名和密码信息的消息头格式不正确");
}
}
else
{
throw new ApplicationException ("请提交包含用户名和密码信息的消息头");
}
}
};
}
注意的是,这个请求必须要请求提交SoapHeader,其中的[System.Web.Script.Services.ScriptService]
这句是利用AJAX.NET处理JSON请求的,如果不需要就免了,如果需要的话下载AJAX.NET,然后在BIN里面放System.Web.Extensions.Design.dll,System.Web.Extensions.dll,按照AJAX.NET默认的那个WEB.CONFIG修改你的web.config,在浏览器中查看*.ASMX文件,如果使用?wsdl可以看到xml的wsdl的话第一步算成功了。
其中注意的是:
Web.Config
在httpHandler中有两个节点很重要:
这两句声明让ScriptHandlerFactory处理WebService请求。
利用ajax请求的时候 http_request.setRequestHeader("Content-Type", "application/json");
加上这句默认的返回的就是JSON。
附上web.CONFIG和相关的dll文件吧:
c# json
在c#代码创建的时候道理一样。
代码如下:
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="一个返回用户资料,订单信息的WebService,请求的手机号码最长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 ="输入单个用户的int值ID,返回用户类",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 ="输入某个用户的手机号码,返回用户类",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 ="输入某个用户的手机号码,返回订单数组",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 ="输入某个用户的ID,返回订单数组",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("用户名[{0}]或密码[{1}]错误",UserName,PassWord));
}
}
else
{
throw new ApplicationException ("包含用户名和密码信息的消息头格式不正确");
}
}
else
{
throw new ApplicationException ("请提交包含用户名和密码信息的消息头");
}
}
};
}
注意的是,这个请求必须要请求提交SoapHeader,其中的[System.Web.Script.Services.ScriptService]
这句是利用AJAX.NET处理JSON请求的,如果不需要就免了,如果需要的话下载AJAX.NET,然后在BIN里面放System.Web.Extensions.Design.dll,System.Web.Extensions.dll,按照AJAX.NET默认的那个WEB.CONFIG修改你的web.config,在浏览器中查看*.ASMX文件,如果使用?wsdl可以看到xml的wsdl的话第一步算成功了。
其中注意的是:
Web.Config
在httpHandler中有两个节点很重要:
代码如下:
这两句声明让ScriptHandlerFactory处理WebService请求。
利用ajax请求的时候 http_request.setRequestHeader("Content-Type", "application/json");
加上这句默认的返回的就是JSON。
附上web.CONFIG和相关的dll文件吧:
c# json
在c#代码创建的时候道理一样。