这一次我们用WCF来替代WebService服务.
还是先创建一个WebApplication工程
添加Grid.htm文件,代码如下:
引用的ext-base.js,ext-all.js,resources/css/ext-all.css文件在www.extjs.com中能够下到














创建WCF文件testsvc.svc
using System;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.ServiceModel.Activation;
using System.ServiceModel.Web;
using Component;
using WebApplication1.Dbml;
namespace WebApplication1
...{
[ServiceContract(Namespace = "")]
[AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]
public class testsvc
...{
// 添加 [WebGet] 属性以使用 HTTP GET
[OperationContract]
public void DoWork()
...{
// 在此处添加操作实现
return;
}
// 在此处添加更多操作并使用 [OperationContract] 标记它们
[OperationContract]
[WebGet(ResponseFormat=WebMessageFormat.Json)]
public string GridsJson()
...{
LinqToSqlDataContext db = new LinqToSqlDataContext();
var sou = db.gameinfos.Select(n => n).Take(10);
return sou.toJson();
}
}
}
创建扩展方法ExtendMethod.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.Script.Serialization;
using System.Collections;
using System.Reflection;
namespace Component
...{
public static class ExtendMethod
...{
/**////
/// 返回Json序列
/// parms字典
/// Key:Json对象名
/// Value:Json对象值
///
///
/// 需要加入的对象
///
public static string toJson(this object This,Dictionary
...{
JavaScriptSerializer json = new JavaScriptSerializer();
var ds = new ...{ source=This};

Dictionary

/**////
/// 返回Json序列
/// parms:加入的对象将与this对象同级
/// 未完成
///
///
/// 需要加入的对象
///
public static string toJson(this object This, params object[] parms)
...{
JavaScriptSerializer json = new JavaScriptSerializer();
json.MaxJsonLength = int.MaxValue;
Dictionary
dic.Add("source", This);
foreach (object i in parms)
...{
Type t = i.GetType();
PropertyInfo[] myproperties = t.GetProperties();
dic.Add(myproperties[0].Name, myproperties[0].GetValue(i, null));
}
return json.Serialize(dic).Replace(""","'");
}
}
}
Trackback: http://tb.blog.csdn.net/TrackBack.aspx?PostId=2112683