c# JSON返回格式的WEB SERVICE_C#教程

我贴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#代码创建的时候道理一样。

时间: 2024-09-20 16:43:45

c# JSON返回格式的WEB SERVICE_C#教程的相关文章

详解C#对XML、JSON等格式的解析_C#教程

一.C#对XML格式数据的解析 1.用XMLDocument来解析 XmlDocument xmlDocument = new XmlDocument(); xmlDocumentLoad("testxml"); //创建新节点 XmlElement nn = xmlDocumentCreateElement("image"); nnSetAttribute("imageUrl", "jpg"); XmlNode node =

【ASP.NET Web API教程】6.2 ASP.NET Web API中的JSON和XML序列化

原文:[ASP.NET Web API教程]6.2 ASP.NET Web API中的JSON和XML序列化 谨以此文感谢关注此系列文章的园友!前段时间本以为此系列文章已没多少人关注,而不打算继续下去了.因为文章贴出来之后,看的人似乎不多,也很少有人对这些文章发表评论,而且几乎无人给予"推荐".但前几天有人询问为何很久没有更新,这让我感觉把这文章翻译出来还是有价值的.为此,本人打算将此工作继续下去.这些关于Web API的技术文章均由微软专业人员撰写,虽然文章作为博客帖子而写得比较简单

Json返回时间的格式中出现乱码问题的两种解决方案_实用技巧

前言:这段时间一直没有写博客,首先是我正在实现权限系列的绝色和操作的实现,因为这些东西在前面我们都已经说过了,所以我们就不重复的说这些了,那么我们知道,在我们使用Json返回数据的时候时间的格式一般都会变了,变成我们不认识的一些字符,那么当我们遇到这些问题的时候我们该怎么解决呢,今天我就来小说一下这个的解决方法. .发现问题 (1).正如我们在前言里面所说,我们在编写Json解析时间的时候会返回一些莫名其妙的东西,那么我们是如何解决这个问题的呢?我现在有两种方法可以解决这个问题,下面我们首先来说

【ASP.NET Web API教程】6.3 内容协商

原文:[ASP.NET Web API教程]6.3 内容协商 本文是Web API系列教程的第6.3小节 6.3 Content Negotiation 6.3 内容协商 摘自:http://www.asp.net/web-api/overview/formats-and-model-binding/content-negotiation By Mike Wasson|May 20, 2012 作者:Mike Wasson | 日期:2012-3-20 This article describe

【ASP.NET Web API教程】1.1 第一个ASP.NET Web API

原文 [ASP.NET Web API教程]1.1 第一个ASP.NET Web API Your First ASP.NET Web API (C#)第一个ASP.NET Web API(C#) By Mike Wasson|January 21, 2012作者:Mike Wasson | 日期:2012-1-21 本文引自:http://www.asp.net/web-api/overview/getting-started-with-aspnet-web-api/tutorial-your

【ASP.NET Web API教程】6.1 媒体格式化器

原文:[ASP.NET Web API教程]6.1 媒体格式化器 6.1 Media Formatters 6.1 媒体格式化器 本文引自:http://www.asp.net/web-api/overview/formats-and-model-binding/media-formatters By Mike Wasson|March 8, 2012 作者:Mike Wasson|日期:2012-3-8 This tutorial shows how support additional me

【ASP.NET Web API教程】3.2 通过.NET客户端调用Web API(C#)

原文:[ASP.NET Web API教程]3.2 通过.NET客户端调用Web API(C#) 注:本文是[ASP.NET Web API系列教程]的一部分,如果您是第一次看本博客文章,请先看前面的内容. 3.2 Calling a Web API From a .NET Client (C#) 3.2 通过.NET客户端调用Web API(C#) 本文引自:http://www.asp.net/web-api/overview/web-api-clients/calling-a-web-ap

【ASP.NET Web API教程】2.4 创建Web API的帮助页面

原文:[ASP.NET Web API教程]2.4 创建Web API的帮助页面 注:本文是[ASP.NET Web API系列教程]的一部分,如果您是第一次看本博客文章,请先看前面的内容. 2.4 Creating a Help Page for a Web API 2.4 创建Web API帮助页面 本文引自:http://www.asp.net/web-api/overview/creating-web-apis/creating-a-help-page-for-a-web-api By

【ASP.NET Web API教程】3.3 通过WPF应用程序调用Web API(C#)

原文:[ASP.NET Web API教程]3.3 通过WPF应用程序调用Web API(C#) 注:本文是[ASP.NET Web API系列教程]的一部分,如果您是第一次看本博客文章,请先看前面的内容. 3.3 Calling a Web API From a WPF Application (C#) 3.3 通过WPF应用程序调用Web API(C#) 本文引自:http://www.asp.net/web-api/overview/web-api-clients/calling-a-we