How does ASP.NET MVC make it easier to create purpose-built, content/device-specific views?
One of the benefits of ASP.NET MVC I keep reading about is that it's easier (than with traditional ASP.NET) t开发者_StackOverflow社区o 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?
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.
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.
精彩评论