简体   繁体   中英

Get value of ActionResult in C# mvc 4

I want to get the "Value" of an actionresult. the actionresult can be a contentresult, a jsonresult or any other type of actionresult.

My current understanding of ActionResult is, that it will be converted to a string when sending back to the client.

eg Content("test") = a simple String that contains test

JSon(object) = a string that represents the object in the JSon format

EDIT: so any Result will be converted to a string - and i want to get this string, so i can manipulate it and return this, manipulated, value.

Note: i am currently trying to write a "framework" that automatically encrypts and decrypts data between client and server, without the user of this framework having to deal with much crypto stuff. This is the reason why i need the "value" of the Actionresult, so i can encrypt it and then send it back to the client!

I hope you can understand my problem now.

My current code looks like this:

 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
        }

In my oppinion you are doing something wrong, controller should be responsible about returning presentation data it could be html, xml, or json but if you want get data from it then you should do layer for that, and call that method in both actions.

So you should have class which represents business logic, and controllers should talk to that classes. In addition each controller should not know about other controller, but can have reference to BLL (business logic)

But still to solve your issue try

((ViewResult)ar ).ViewData.Model

Use filter in this case. I've seen an example of someone modifying the output stream ("Value" like you called it) in this post http://arranmaclean.wordpress.com/2010/08/10/minify-html-with-net-mvc-actionfilter/

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM