发布于 2016-06-12 23:08:50 | 122 次阅读 | 评论: 0 | 来源: 网友投递
ASP.NET
ASP.NET 是.NET FrameWork的一部分,是一项微软公司的技术,是一种使嵌入网页中的脚本可由因特网服务器执行的服务器端脚本技术,它可以在通过HTTP请求文档时再在Web服务器上动态创建它们。 指 Active Server Pages(动态服务器页面) ,运行于 IIS(Internet Information Server 服务,是Windows开发的Web服务器)之中的程序 。
示例代码下载: http://zsharedcode.googlecode.com/files/JQueryElementDemo.rar
本文中所包含的内容如下:
* 准备
* 一般处理程序/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, 可以在 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; }
}
}
function(data){
alert(data.message);
}
<?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>
<%@ 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;
}
}
function(data){
alert(data.message);
}
function(data){
alert(data.d.message);
}
JQueryElement 是开源共享的代码, 可以在 http://code.google.com/p/zsharedcode/wiki/Download 页面下载 dll 或者是源代码.
实际过程演示: http://www.tudou.com/programs/view/rHCYHX9cmcI/, 建议全屏观看.