Chapter 17. Advanced view techniques

 

This chapter covers

  • Using layouts to craft site-wide templates
  • Applying partials for shared snippets of content
  • Leveraging child actions for common widgets
  • Eliminating subtle URL-generation
  • Examining alternative view engines with the Spark view engine

The MVC pattern gives us separation of concerns between the model, controller, and view, but this pattern didn’t eliminate the need for developers to carefully design their views. You saw in the last chapter how you can use controller extension points to build clean, easy-to-modify controllers. With the elimination of code-behind and the addition of a view model object, you can focus strictly on rendering content inside your view. But without careful attention, your views can still slide into a morass of duplication and spaghetti code. You can no longer lean on custom controls to encapsulate view behavior as you did in Web Forms. Instead, ASP.NET MVC provides similar and expanded mechanisms for tackling all levels of duplication in your views.

In this chapter, we’ll first explore various means of eliminating the various forms of duplication in our applications. Next, we’ll examine how subtle bugs can arise when generating URLs for action methods that include parameters, and we’ll look at a strategy for eliminating those bugs. Finally, we’ll take a tour of the Spark view engine and see how its syntax and capabilities make it an excellent alternative to the built-in view engines.

17.1. Eliminating duplication in the view

17.2. Building query-string parameter lists

17.3. Exploring the Spark view engine

17.4. Summary