c# JSON c# JSON返回格式的WEB SERVICE

软件发布|下载排行|最新软件

当前位置:首页IT学院IT技术

c# JSON c# JSON返回格式的WEB SERVICE

  2021-03-17 我要评论
想了解c# JSON返回格式的WEB SERVICE的相关内容吗,在本文为您仔细讲解c# JSON的相关知识和一些Code实例,欢迎阅读和指正,我们先划重点:c#,JSON,WEB,SERVICE,下面大家一起来学习吧。
我贴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#代码创建的时候道理一样。

Copyright 2022 版权所有 软件发布 访问手机版

声明:所有软件和文章来自软件开发商或者作者 如有异议 请与本站联系 联系我们