【发布时间】:2014-01-20 21:32:14
【问题描述】:
我想获得动作结果的“价值”。 actionresult 可以是 contentresult、jsonresult 或任何其他类型的 actionresult。
我目前对ActionResult的理解是,发送回客户端时会转成字符串。
例如Content("test") = 一个包含 test 的简单字符串
JSon(object) = 以 JSon 格式表示对象的字符串
编辑: 所以任何 Result 都将被转换为一个字符串 - 我想得到这个字符串,所以我可以操纵它并返回这个操纵的值。
注意: 我目前正在尝试编写一个“框架”,它可以自动加密和解密客户端和服务器之间的数据,而该框架的用户不必处理很多加密的东西。这就是为什么我需要 Actionresult 的“值”,所以我可以对其进行加密,然后将其发送回客户端!
希望你现在能理解我的问题。
我当前的代码如下所示:
public ActionResult PerformChange(String action, String controller)
{
RedirectToRouteResult res = RedirectToAction(action, controller);
//how can i get the value of this RedirectToRouteResult? e.g. the string
Assembly cur = Assembly.GetExecutingAssembly();
List<Type> controllers = cur.GetTypes().Where(x => x.IsSubclassOf(typeof(Controller))).ToList();
List<Type> controllersWithCorrectName = controllers.Where(x => x.Name == controller).ToList<Type>();
if (controllersWithCorrectName.Count != 1)
{
//return error
return new EmptyResult();
}
Type targetController = controllersWithCorrectName.Single();
MethodInfo[] customMethods = targetController.GetMethods().Where(x => (x.ReturnType == typeof(ActionResult) || x.ReturnType.IsSubclassOf(typeof(ActionResult))) && x.IsPublic).ToArray<MethodInfo>();
List<MethodInfo> methods = customMethods.Where(x => x.Name == action).ToList<MethodInfo>();
if (methods.Count != 1)
{
//return error
return new EmptyResult();
}
MethodInfo targetMethodInfo = methods.Single();
Controller c = (Controller)Activator.CreateInstance(targetController);
ActionResult ar = (ActionResult)targetMethodInfo.Invoke(c, null);
//or how to get rhe value with this way???
//perform change to extracted value
return null; //return changed value
}
【问题讨论】:
-
不确定您的问题是什么。您的示例显示了一些非常简单的反射来调用方法并获得结果(这可能有效)。您的问题是“如何检查对象的类型?”考虑将您拥有的示例替换为您尝试实现/不起作用的示例(并解释什么/如何不起作用)。
-
看来您必须使用过滤器,而不是其他操作
标签: c# asp.net-mvc asp.net-mvc-4