类1:
`namespace layoutTest.MyCls
{
public class Users
{
public string GetUserName(int id)
{
return "testUser" + id.ToString();
}
public string Test(string p1, int p2)
{
return p1 + p2.ToString();
}
}
}`
类2:
namespace layoutTest.MyCls
{
namespace layoutTest.Model
{
[Serializable]
public class Rights
{
public int Id { get; set; }
public int ParentId { get; set; }
public string Name { get; set; }
public string Url { get; set; }
}
}
public class Rights
{
private static List<layoutTest.Model.Rights> _rightsCollections;
public static List<layoutTest.Model.Rights> RightsCollections
{
get
{
if (_rightsCollections == null)
{
_rightsCollections = new List<layoutTest.Model.Rights>();
_rightsCollections.Add(new layoutTest.Model.Rights() { Id = 1, Name = "系统设置" });
_rightsCollections.Add(new layoutTest.Model.Rights() { Id = 2, Name = "菜单配置" });
_rightsCollections.Add(new layoutTest.Model.Rights() { Id = 3, Name = "用户管理", ParentId = 1, Url = "/html/sys/user.html" });
_rightsCollections.Add(new layoutTest.Model.Rights() { Id = 4, Name = "系统菜单", ParentId = 2, Url = "/html/sys/menu.html" });
}
return _rightsCollections;
}
}
public List<object> GetRights(string id=null)
{
id = id == null ? "0" : id;
List<layoutTest.Model.Rights> hls = RightsCollections.FindAll(m => m.ParentId == int.Parse(id));
List<object> list = new List<object>();
foreach (layoutTest.Model.Rights r in hls)
{
List<layoutTest.Model.Rights> tmp = RightsCollections.FindAll(m => m.ParentId == r.Id);
var item = new
{
id = r.Id,
text = r.Name,
tag = r.Url,
state = tmp.Count == 0 ? "open" : "closed"
};
list.Add(item);
}
return list;
}
}
}`
类3(方法映射配置类,可使用xml文档替换映射配置):
namespace layoutTest.MyCls
{
public class PostMethod
{
public string ClassName { get; set; }
public string MethodName { get; set; }
public string KeyName { get; set; }
private static List<PostMethod> _methodCollection;
public static List<PostMethod> MethodCollection
{
get
{
if (_methodCollection == null)
{
_methodCollection = new List<PostMethod>();
_methodCollection.Add(new PostMethod() { KeyName = "getRights", ClassName = "layoutTest.MyCls.Rights", MethodName = "GetRights" });
_methodCollection.Add(new PostMethod() { KeyName = "getUserName", ClassName = "layoutTest.MyCls.Users", MethodName = "GetUserName" });
_methodCollection.Add(new PostMethod() { KeyName = "test", ClassName = "layoutTest.MyCls.Users", MethodName = "Test" });
}
return _methodCollection;
}
}
}
}
控制器:
using System.Reflection;
namespace layoutTest.Controllers
{
public class HomeController : Controller
{
//
// GET: /Home/
public ActionResult Index()
{
return View();
}
/// <summary>
/// 统一接收参数,并调用方法,返回结果,目前不支持数组参数
/// </summary>
/// <param name="para"></param>
/// <returns></returns>
public JsonResult getPost(string para)
{
try
{
Newtonsoft.Json.Linq.JObject obj = (Newtonsoft.Json.Linq.JObject)Newtonsoft.Json.JsonConvert.DeserializeObject(para);
string method = obj.Value<string>("method");
if (method == null)
{
return Json(new { err = "未提供参数:method" });
}
else
{
try
{
MyCls.PostMethod pmethod = MyCls.PostMethod.MethodCollection.Find(m => m.KeyName == method);
Type type = Type.GetType(pmethod.ClassName);
MethodInfo mi = type.GetMethod(pmethod.MethodName);
ParameterInfo[] paras = mi.GetParameters();
Newtonsoft.Json.Linq.JArray jarray = (Newtonsoft.Json.Linq.JArray)obj["paras"];
//foreach(Newtonsoft.Json.Linq.JObject jobj in jarray)
//{
//}
List<object> listValue = new List<object>();
if (jarray.Count > 0)
{
//如果有参数
Newtonsoft.Json.Linq.JToken jtoken = jarray[0];
foreach (ParameterInfo pinfo in paras)
{
foreach (Newtonsoft.Json.Linq.JProperty jp in jtoken)
{
if (jp.Name == pinfo.Name)
{
listValue.Add(Convert.ChangeType(jp.Value, pinfo.ParameterType));
break;
}
}
}
}
else
{
//没有参数的时候需要判断是否有form提交过来的值
bool isAdd = false;
if (Request.Form.Count > 0)
{
//获取easyui控件默认传递的值,如rows,page,id等
foreach (ParameterInfo pinfo in paras)
{
if (Request.Form[pinfo.Name] != null)
{
listValue.Add(Request.Form[pinfo.Name]);
isAdd = true;
}
}
}
if(!isAdd)
{
//如果没有默认传递的值,就使用默认值
foreach (ParameterInfo pinfo in paras)
{
listValue.Add(pinfo.DefaultValue);
}
}
}
Assembly asm = Assembly.GetExecutingAssembly();
object clsObj = asm.CreateInstance(pmethod.ClassName);
object objResult = mi.Invoke(clsObj, listValue.ToArray());
return Json(objResult);
}
catch (Exception ex)
{
return Json(new { err = ex.Message });
}
}
}
catch (Exception ex)
{
return Json(new { err = ex.Message });
}
}
}
[Serializable]
public class PostData
{
public string method { get; set; }
public object paras { get; set; }
}
}
视图:
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width" />
<title>Index</title>
<script src="~/Scripts/jquery-1.11.3.js"></script>
<script src="~/Scripts/jquery-1.11.3.min.js"></script>
<link rel="stylesheet" type="text/css" href="~/Content/themes/default/easyui.css" />
<link rel="stylesheet" type="text/css" href="~/Content/themes/icon.css" />
<script type="text/javascript" src="~/Scripts/jquery.easyui-1.4.5.js"></script>
<script type="text/javascript" src="~/Scripts/jquery.easyui-1.4.5.min.js"></script>
<script>
$(document).ready(function () {
var data = {
method: 'getRights',
paras: []
};
var p = JSON.stringify(data);
$('#umenu').tree({
url: '/home/getpost',
queryParams: { para: p },
onSelect: function (node) {
},
onClick: function (node) {
console.log(node);
if (node.tag != "") {
//var p = umenu.tree('getParent', node.target);
//var title = node.text;
//if (p) {
// title = p.text + ' -> ' + title;
//}
//change(title, node.tag);
}
},
onLoadSuccess: function (e, data) {
if (data["err"]) {
alert(data["err"]);
}
}
});
})
</script>
</head>
<body class="easyui-layout">
<div region="west" title="菜单栏" split="true" style="width:280px;padding1:1px;overflow:hidden;">
<ul id="umenu"></ul>
</div>
<div region="center" title="功能区">
<div class="easyui-tabs" id="centerTab" fit="true" border="false">
<div title="欢迎页" style="padding:20px;overflow:hidden;">
<div style="margin-top:20px;">
<h3>你好</h3>
</div>
</div>
</div>
</div>
</body>
</html>
加参数访问的示例:
//var data = {
// method: 'getRights',
// paras: [{
// p1: 'ddd',
// p2: 2
// }, {
// p1: 'ttt',
// p2:4
// }]
//};
//var p = JSON.stringify(data);
//$.post('/home/getpost', { para: p }, function (data) {
// if (data["err"]) {
// alert(data["err"]);
// } else {
// alert(data);
// }
//});