views:

534

answers:

3

Currently I am using the terrific Linq 2 Json.net (by newtonsoft), which is a very great simple tool to generate JSON result in programatic way.

But after finishing some projects, I stopped and rethink, should I generate the JSON result in the controller? I mean, in .net MVC framework, it DOES provided a JSONResult as one of the ViewResult. But should the controller bother how the result is generated? Or should it just "Provide" the data to view, and it should be the view's job to generate the necessary output (and formatting)?

One last thing, I also heard that using "ViewData" in controller might not be a good idea as controller is concerning too much on display/output issue, any better pattern or method could be use?

A: 

I usually set my results for JsonResults in the controller itself. I feel it's up to the Model/DAL/BLL to give me the data/ienumerable filtered as requested, but the controller/view frob & return it. In the case if a JsonResult, the framework handles the view/encoding piece. I would reserve views for formatted textual (html mainly) output. Use the built in handler/result for JSON and File/Image responses. XML output can reall go either way imo.

Tracker1
I agree views are for formatted output, but JSON isn't it a type of format output? It sounds to me that the JSON output for controller really doing two things, process input and format output at the same time.
xandy
+1  A: 

JSON output is fairly simple with most server side languages, I've never had a reason (or been able to justify attempts at) to complicate them with templates.

While you can, the amount of overhead incurred is probably a waste. In most cases, rendering templates triggers an entire subsystem that has to be wound-up before even getting to business.

The whole idea (to me) about JSON (and AJAX responses in general) is that you can shave a ton of overhead off of your server.

Omega
This is a Rails-specific note...While working with content negotiation, it is possible to use the templating language to create JSON responses.Ultimately though, those templates are dead simple. I think this would apply to any other language where templates were used to generate JSON output.
Omega
+1  A: 

I think I will justify the JSON output in Controller as JSON is just a form of ViewData, just similar thing as using ViewData Dictionary to communicate with the View pages.

And the actual View page is already rendered, or handled by the client side languages. Although one downside is, the JSON-output Controller is quite dependent to the view, yes, you can still change the view to some other thing which accepts that JSON as channel of communication, but not a good idea if you want to change the client to, for example, a desktop application using other channels as communication (like, direct TCP connection nor SOAP application etc.) coz the controller is made for JSON. (true you can make an adapter to do translation).

So to wrap it up, JSON rendering in controller is OK as long as you are not planning to other platform while presisting the controller unchanged.

xandy