Get result from an ActionResult directly in the controller
I want to be able to get the result that the Acti开发者_开发技巧onResult
will generate directly in my controller for debugging purposes. How do I do that?
Something like:
public ActionResult Parts(string id)
{
var parts = _repository.GetParts(id);
var action = Json(parts);
var generatedJson = XXXXX;
return action;
}
For debugging purposes you definitely want to use a debugging tool such as FireBug or Fiddler, but if you insist pollutingmodifying your source code when in Debug mode you could take a look at the JavaScriptSerializer class (which is internally used by the JsonResult class):
var generatedJson = new JavaScriptSerializer().Serialize(parts);
Since you are returning Json and I assume you want to look at the result, you can use a json viewer plugin in FireFox, jsonview. At least that's how I do it.
In case it's for some other reason, please specify what you want to do exactly.
Call ExecuteResult on the result.
But if you want to inspect the json returned by it, use the browser debugging tools along with some json viewer add-on.
you could also use something like NLog and bind it to ILogger in your global.asax. thereafter, you can temporarily send any json related controller actions out to the log file for later inspection.
another way is to use an actionfilter and decorate the action that you wanna capture (these could be saved in a log file or some other visual rendering device). there's a good example of this that you could adapt for your purposes here:
http://binary-studio.com/blog/technical-blog/asp-net-mvc-custom-action-filters/
hope this helps..
精彩评论