使用ASP.NET一般處理程序或WebService返回JSON的實(shí)現(xiàn)代碼
示例代碼下載: http://zsharedcode.googlecode.com/files/JQueryElementDemo.rar
本文中所包含的內(nèi)容如下:
* 準(zhǔn)備
* 一般處理程序/ashx
* WebService/asmx準(zhǔn)備
如果希望通過 ashx 或者 asmx 來返回 JSON, 那么需要引用程序集 System.Web.Extensions.dll, 在 .NET 3.5, 4.0 中已經(jīng)默認(rèn)包含. 對于 .NET 2.0, 3.0, 需要安裝 ASP.NET 2.0 AJAX, 可以在 http://www.microsoft.com/download/en/details.aspx?displaylang=en&id=883 下載.
一般處理程序/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 方法, 將對象轉(zhuǎn)化為 JSON 對應(yīng)的字符串. 而轉(zhuǎn)化的對象是 SortedDictionary, 會生成 { "message": "你好 x, 20xx-xx-xx xx:xx:xx" } 這樣類似的字符串. 如果需要返回?cái)?shù)組, 可以定義 object[] 來轉(zhuǎn)換. 代碼中還使用了 context.Response.Cache.SetNoStore ( ); 來讓瀏覽器每次請求 ashx 時都重新訪問, 而不是使用緩存.
如果使用 jQuery, 可以使用下面的函數(shù)來接收 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 來調(diào)用這些方法. 這里不需要再使用 JavaScriptSerializer 將對象轉(zhuǎn)化為 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 中使用下面的函數(shù)來接受 JSON:
function(data){
alert(data.message);
}
function(data){
alert(data.d.message);
}
JQueryElement 是開源共享的代碼, 可以在 http://code.google.com/p/zsharedcode/wiki/Download 頁面下載 dll 或者是源代碼.
實(shí)際過程演示: http://www.tudou.com/programs/view/rHCYHX9cmcI/, 建議全屏觀看.
- C# .Net動態(tài)調(diào)用webService實(shí)現(xiàn)思路及代碼
- asp.net(c#)動態(tài)修改webservice的地址和端口(動態(tài)修改配置文件)
- C#發(fā)送HttpPost請求來調(diào)用WebService的方法
- C#動態(tài)webservice調(diào)用接口
- c# JSON返回格式的WEB SERVICE
- c#動態(tài)調(diào)用Webservice的兩種方法實(shí)例
- c#動態(tài)改變webservice的url訪問地址
- .net實(shí)現(xiàn)webservice簡單實(shí)例分享
- .NET C#創(chuàng)建WebService服務(wù)簡單實(shí)例
相關(guān)文章
C#(.NET)數(shù)據(jù)訪問連接、查詢、插入等操作的封裝類
一個C#(.NET)數(shù)據(jù)訪問連接、查詢、插入等操作的封裝類2008-05-05EasyUI Tree+Asp.net實(shí)現(xiàn)權(quán)限樹或目錄樹導(dǎo)航的簡單實(shí)例
本篇文章主要是對EasyUI Tree+Asp.net實(shí)現(xiàn)權(quán)限樹或目錄樹導(dǎo)航的簡單實(shí)例進(jìn)行了介紹,需要的朋友可以過來參考下,希望對大家有所幫助2014-02-02ASP.NET?MVC5網(wǎng)站開發(fā)用戶登錄、注銷(五)
這篇文章主要介紹了ASP.NET?MVC5?網(wǎng)站開發(fā)中用戶登錄、注銷的實(shí)現(xiàn)功能,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2015-09-09ASP.NET動態(tài)生成靜態(tài)頁面的實(shí)例代碼
生成靜態(tài)頁有很多好處,可以緩解服務(wù)器壓力、方便搜索網(wǎng)站搜索等等,下面介紹一下生成靜態(tài)頁的實(shí)例代碼,有需要的朋友可以參考一下2013-07-07visual Studio 2017創(chuàng)建簡單控制臺程序
這篇文章主要為大家詳細(xì)介紹了visual Studio 2017創(chuàng)建簡單控制臺程序,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-11-11.NET6+Quartz實(shí)現(xiàn)定時任務(wù)的示例詳解
在實(shí)際工作中,經(jīng)常會有一些需要定時操作的業(yè)務(wù),如:定時發(fā)郵件,定時統(tǒng)計(jì)信息等,那么如何實(shí)現(xiàn)才能使得我們的項(xiàng)目整齊劃一呢?本文通過一些簡單的小例子,簡述在.Net6+Quartz實(shí)現(xiàn)定時任務(wù)的一些基本操作,如有不足之處,還請指正2023-03-03.Net RabbitMQ實(shí)現(xiàn)HTTP API接口調(diào)用
RabbitMQ Management插件還提供了基于RESTful風(fēng)格的HTTP API接口來方便調(diào)用。本文就主要介紹了.Net RabbitMQ實(shí)現(xiàn)HTTP API接口調(diào)用,感興趣的可以了解一下2021-06-06