手机
当前位置:查字典教程网 >编程开发 >C#教程 >c# JSON返回格式的WEB SERVICE
c# JSON返回格式的WEB SERVICE
摘要:我贴c#的代码:复制代码代码如下:namespaceIWebs.Webs{usingSystem;usingSystem.Web.Servi...

我贴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中有两个节点很重要:

复制代码 代码如下:

<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"/>

这两句声明让ScriptHandlerFactory处理WebService请求。

利用ajax请求的时候 http_request.setRequestHeader("Content-Type", "application/json");

加上这句默认的返回的就是JSON。

附上web.CONFIG和相关的dll文件吧:

c# json

在c#代码创建的时候道理一样。

【c# JSON返回格式的WEB SERVICE】相关文章:

C#缩略图多路径多格式保存的实例

基于为何我不喜欢用Path.Combine的详解

C#编写的windows计算器的实例代码

基于C# MBG 扩展方法类库的使用详解

C# 视频播放类

C#中把日志导出到txt文本的简单实例

c# 适配器模式

C# 委托(delegate) 的小例子

C# 泛型数组学习小结

C#: 引用变量与数值变量的区别

精品推荐
分类导航