public class Handler : IHttpHandler, IRequiresSessionState
string method = Request["METHAD"];//namespace.class.method//包名.类名.方法名
string ClassNamespace = method.Substring(0, method.LastIndexOf("."));// "</SPAN>< style="FONT-FAMILY: 宋体; COLOR: green; FONT-SIZE: 9pt" SPAN >?;< SPAN>
object objType = CreateObjectNoCache(Request["DLL"], ClassNamespace);//反射
Type ht = objType.GetType();
List<string> likey = new List<string>();
foreach (string item in Request.Params.AllKeys)
{
if (item.StartsWith("[__DOTNET__]"))
likey.Add(item);
}
int length = likey.Count;//Request.Form.AllKeys.Length;
//int length=Request
//反射方法参数,和类型
object[] obj = new object[length];// 数据
Type[] objTypes = new Type[length];//类型
for (int i = 0; i < length; i++)
{
string ReqKey = likey[i].Substring(12);//Request.Form.AllKeys[i];
Type tType = null;
if (ReqKey.IndexOf("[") != -1)
{
tType = Type.GetType(ReqKey.Remove(ReqKey.LastIndexOf("[")), true, true);
}
else
{
tType = Type.GetType(ReqKey, true, true);
}
objTypes[i] = tType;//类型赋值
//如果它是值类型,或者String
if (tType.Equals(typeof(string)) || tType.Equals(typeof(string[])) || (!tType.IsInterface && !tType.IsClass))
{
obj[i] = Convert.ChangeType(Request.Params[likey[i]], tType);
}
else if (tType.IsClass)//如果是类,将它的json字符串转换成对象
{
obj[i] = JavaScriptConvert.DeserializeObject(Request.Params[likey[i]], tType); //Serialize.Deserialize(Request.Form[i], tType);
}
}
//TODO
MethodInfo methodInfo = ht.GetMethod(method.Substring(method.LastIndexOf(".") + 1), objTypes);
object returnValue = null;
foreach (object attribute in methodInfo.GetCustomAttributes(true))
{
if (attribute is AjaxMethod)
{
returnValue = methodInfo.Invoke(objType, obj);
break;
}
}
//执行方法
//object returnValue = methodInfo.Invoke(objType, obj);
if (returnValue == null)
throw new Exception("你没用权限执行该方法,是否加了[XAjaxMethod]自定义属性");
#region 处理返回值
if (returnValue.GetType().IsClass && !returnValue.GetType().Equals(typeof(string)))
{
if (returnValue.GetType().Equals(typeof(XmlDocument)))
{
//如果是XML文档,则写入xml文档
Response.ContentType = "text/xml";
Response.Write(((XmlDocument)returnValue).OuterXml);
}
else if (returnValue.GetType().IsClass && returnValue.GetType().Equals(typeof(DataTable)))
{
if (!string.IsNullOrEmpty(Request["callback"]) && Request["callback"].StartsWith("jsonp"))
{
Response.ContentType = "application/json";
Response.ContentEncoding = Encoding.Default;
Response.Write(Request["callback"] + "(" + Json.DataTableToJson("Rows", (DataTable)returnValue) + ")");
}
else
Response.Write(Json.DataTableToJson("Rows", (DataTable)returnValue));
}
else
{
if (!string.IsNullOrEmpty(Request["callback"]) && Request["callback"].StartsWith("jsonp"))
{
Response.ContentType = "application/json";
Response.ContentEncoding = Encoding.Default;
Response.Write(Request["callback"] + "(" + JavaScriptConvert.SerializeObject(returnValue) + ")");
}
else
Response.Write(JavaScriptConvert.SerializeObject(returnValue));
}
}
else
{
if (!string.IsNullOrEmpty(Request["callback"]) && Request["callback"].StartsWith("jsonp"))
{
Response.ContentType = "application/json";
Response.ContentEncoding = Encoding.Default;
Response.Write(Request["callback"] + "(\"" + returnValue.ToString() + "\")");
}
else
Response.Write(returnValue.ToString());
}
#endregion
public class AjaxMethod : Attribute
{
public override string ToString()
{
return "XAjaxMethod";
}
}
<httpHandlers>
<add path="DotNet2.ashx" verb="*" type="AjaxHandler.Handler,AjaxHandler"/>
</httpHandlers>
$.ajax({
url: "/DotNet2.ashx?METHAD=AjaxTest._Default.Test&DLL=AjaxTest",
data: {
"[__DOTNET__]System.String": "测试测试一"
},
success: function(json) {
alert(json);
}
});
METHAD:命名空间.类名.方法名
DLL:DLL名称
data: {
"[__DOTNET__]System.String": "测试测试二",
"[__DOTNET__]System.Int32": "12345678"
}
需要加上[__DOTNET__]前缀,后面是参数类型,前缀是为了处理get方式