views:

62

answers:

2

One of the benefits of ASP.NET MVC I keep reading about is that it's easier (than with traditional ASP.NET) to create content-specific and/or device-specific views, e.g. straight HTML or JSON or XML or iPhone-targed HTML, etc. I do see how the more structured separation of view logic from controller logic facilitates this.

Let's assume though we have a traditional ASP.NET application with a reasonably implemented architecture (ha!) that actually separates business logic from view logic and whose .aspx files aren't spaghetti code or mangled with markup intermixed with business logic and database access and other stuff which shouldn't be there. Given a traditional ASP.NET application constructed like this, is it still more difficult to create these clean, purpose-built views than in an ASP.NET MVC app?

Or, is it just a matter of ASP.NET MVC, by virtue of the MVC pattern and the better-enforced separation between the model, view and controller logic that steers developers towards writing cleaner code with better separation of concerns and hence makes it easier to create these purpose-built views?

+1  A: 

I believe the ASP.NET MVC framework (not necessarily the pattern) makes it easier to create purpose built views mainly because you can make a request to the exact same URL and depending on the request header you return an ActionResult that that is tailored specifically for that request.

Webforms makes this a little more difficult as you're generally requesting a specific resource (i.e. an aspx page) rather than requesting a URL. When you request that resource the webforms engine makes it a lot harder to return something other than the requested resource.

Having said that, it's not impossible with webforms. You can do something similar with webforms where you can modify the response headers and return something completely different, however it's not quite as intuitive as it is in the MVC framework.

lomaxx
lomaxx and Bit Destroyer - I see what you mean, in fact, you helped me realize that I was thinking about this question in a manner more complicated than it needed to be. I now see that the answer is a function of ASP.NET MVC's decoupling of the "resource requested" from the "result", and how, as Bit Destroyer" points out below, you get extra goodies thrown in such as the built in ability to serialize the result to JSON.
Matt
+1  A: 

ASP.NET does make it a lot easier because, as lomaxx said, the URL isn't statically tied to a particular resource. MVC provides methods on the base controller class that allow you to return ActionResults of different types. For example, if you return an ActionResult from the Json helper method with the objects you want to return as arguments, it will format them into Json data and present them accordingly. I'm sure there's probably a helper for Xml too, but I personally haven't needed it thus far.

This article by Scott Hanselman describes how to create custom views for specific mobile devices. I haven't used his tutorial yet, but it seems pretty straight forward. It allows you to detect what capabilities the device being browsed on supports which allows you to create views that are custom tailored to work on many mobile devices.

Bit Destroyer
Bit Destroyer - Please see my comment to lomaxx below as I owe you credit for my realization as well. I accepted his answer b/c your answers were similar and his was first, but I upvoted yours because you made the good point about the json helper and the article you linked to.
Matt