当前位置: 移动技术网 > IT编程>开发语言>.net > 使用ASP.NET一般处理程序或WebService返回JSON的实现代码

使用ASP.NET一般处理程序或WebService返回JSON的实现代码

2017年12月12日  | 移动技术网IT编程  | 我要评论

曾小轩,优雅首席的亿万娇宠,dnf幽灵猎人套

示例代码下载:

本文中所包含的内容如下:

  * 准备
  * 一般处理程序/ashx
  * webservice/asmx准备

如果希望通过 ashx 或者 asmx 来返回 json, 那么需要引用程序集 system.web.extensions.dll, 在 .net 3.5, 4.0 中已经默认包含. 对于 .net 2.0, 3.0, 需要安装 asp.net 2.0 ajax, 可以在 下载.

一般处理程序/ashx

使用一般处理程序返回 json, 对于不同版本的 .net 都是类似, 请看下面的 handler.ashx 的代码:

复制代码 代码如下:

<%@ webhandler language="c#" class="handler" %>
using system;
using system.web;
using system.web.script.serialization;
using system.collections.generic;
public class handler : ihttphandler
{
public void processrequest(httpcontext context)
{
context.response.contenttype = "text/javascript";
context.response.cache.setnostore ( );
string name = context.request["name"];
sorteddictionary<string, object> values = new sorteddictionary<string, object>();
values.add("message",
string.isnullorempty(name) ? "无名氏" :
string.format("你好 {0}, {1}", name, datetime.now));
context.response.write(new javascriptserializer().serialize(values));
}
public bool isreusable
{
get { return false; }
}
}

上面的例子中, 通过 javascriptserializer 类的 serialize 方法, 将对象转化为 json 对应的字符串. 而转化的对象是 sorteddictionary, 会生成 { "message": "你好 x, 20xx-xx-xx xx:xx:xx" } 这样类似的字符串. 如果需要返回数组, 可以定义 object[] 来转换. 代码中还使用了 context.response.cache.setnostore ( ); 来让浏览器每次请求 ashx 时都重新访问, 而不是使用缓存.
如果使用 jquery, 可以使用下面的函数来接收 json:
复制代码 代码如下:

function(data){
alert(data.message);
}

webservice/asmx
在不同版本的 .net 中, 通过 javascript 访问 webservice 并返回 json 是略有不同的. 首先, 可以分别采用不同的 web.config 文件.法全部列出, 如有需要请参考:
.net 2.0, 3.0 web.config
复制代码 代码如下:

<?xml version="1.0"?>
<configuration>
<system.web>
<compilation debug="true">
<assemblies>
<add
assembly="system.web.extensions, version=1.0.61025.0, culture=neutral, publickeytoken=31bf3856ad364e35"/>
</assemblies>
</compilation>
<pages/>
<httphandlers>
<remove verb="*" path="*.asmx"/>
<add verb="*" path="*.asmx"
type="system.web.script.services.scripthandlerfactory"
validate="false"/>
</httphandlers>
</system.web>
</configuration>

.net 3.5 web.config
.net 4.0 web.config
以上两个版本的 web.config 可以在示例压缩包中的 web.3.5.config 和 web.4.config 中查看.
下面是 webservice.asmx/webservice.cs 的代码:
复制代码 代码如下:

<%@ webservice language="c#" codebehind="~/app_code/webservice.cs" class="webservice" %>
using system;
using system.web;
using system.web.services;
using system.web.services.protocols;
using system.web.script.services;
using system.web.script.serialization;
using system.collections.generic;
[webservice ( namespace = "http://tempuri.org/" )]
[webservicebinding ( conformsto = wsiprofiles.basicprofile1_1 )]
[scriptservice]
public class webservice : system.web.services.webservice
{
[webmethod]
[scriptmethod]
public sorteddictionary<string, object> save ( string name )
{
this.context.response.cache.setnostore ( );
sorteddictionary<string, object> values = new sorteddictionary<string, object> ( );
values.add ( "message",
string.isnullorempty ( name ) ? "无名氏" :
string.format ( "你好 {0}, {1}", name, datetime.now ) );
return values;
}
}

为类添加属性 scriptservice, 并对类中的方法使用属性 scriptmethod, 可以让 javascript 来调用这些方法. 这里不需要再使用 javascriptserializer 将对象转化为 json 字符串, 而是直接返回对象即可. 上面的代码中返回了 sorteddictionary, 在 .net 2.0, 3.0 中将类似于 { "message": "你好 x, 20xx-xx-xx xx:xx:xx" } 的形式, 而对于 .net 3.5, 4.0 则是 { "d": { "message": "你好 x, 20xx-xx-xx xx:xx:xx" } }, 因此可以分别在 jquery 中使用下面的函数来接受 json:
复制代码 代码如下:

function(data){
alert(data.message);
}
function(data){
alert(data.d.message);
}

jqueryelement 是开源共享的代码, 可以在 页面下载 dll 或者是源代码.

实际过程演示: , 建议全屏观看.

欢迎访问 panzer 开源项目, http://zsharedcode.googlecode.com/ , 其中包含了 iebrowser 控制 webbrowser 执行各种 js 和 jquery 脚本以及录制功能 和 jqueryui 的 asp.net 控件 jqueryelement.

如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复

相关文章:

验证码:
移动技术网