jgauffin
jgauffin

Reputation: 101192

Get result from an ActionResult directly in the controller

I want to be able to get the result that the ActionResult 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;
}

Upvotes: 2

Views: 1269

Answers (4)

Darin Dimitrov
Darin Dimitrov

Reputation: 1039508

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);

Upvotes: 2

jim tollan
jim tollan

Reputation: 22485

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..

Upvotes: 0

hazimdikenli
hazimdikenli

Reputation: 6029

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.

Upvotes: 0

Morph
Morph

Reputation: 1719

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.

Upvotes: 0

Related Questions