You are previewing Programming Microsoft® ASP.NET MVC.
O'Reilly logo
Programming Microsoft® ASP.NET MVC

Book Description

Author Dino Esposito leads you through the features, principles, and pillars of the ASP.NET MVC framework, demonstrating how and when to use this model to gain full control of HTML, simplify testing, and design better Web sites and experiences.

Table of Contents

  1. Programming Microsoft® ASP.NET MVC
  2. Dedication
  3. A Note Regarding Supplemental Files
  4. Acknowledgments
  5. Introduction
    1. ASP.NET MVC and Web Forms
    2. Who Is This Book For?
    3. Companion Content
    4. Hardware and Software Requirements
    5. Support for This Book
      1. We Want to Hear from You
  6. I. The Programming Paradigm
    1. 1. Goals of ASP.NET MVC and Motivation for Its Development
      1. The Deep Impact of ASP.NET
        1. Productivity Is King
          1. The Fast-Growing Web Industry
          2. Adapting the RAD Model to the Web
          3. Engineering Current Best Practices
        2. The Web Forms Model
          1. Page Postbacks
          2. That Controversial Big Thing Named View State
          3. Server Controls
        3. The "Page Controller" Pattern
          1. The HTTP Handler
          2. The Code-Behind Class
          3. Page Hierarchies
      2. The ASP.NET Age of Reason
        1. ASP.NET's Signs of Aging
          1. Limited SoC
          2. Limited Testability
          3. Limited Control over HTML
          4. Alternative Models Grow Up
        2. The Turning Point
          1. Is a Better ASP.NET Really Possible?
          2. The Model-View-Presenter Pattern in ASP.NET Web Forms
          3. The Web Client Software Factory Experience
      3. ASP.NET MVC at a Glance
        1. ASP.NET MVC Highlights
          1. Underlying Pattern
          2. A RESTful Flavor
          3. Taking Action
          4. Testability
        2. Web Forms vs. ASP.NET MVC
          1. Pros and Cons of Web Forms
          2. Pros and Cons of ASP.NET MVC
          3. Do Not Overlook a Team's Skills and Attitude
          4. Indisputable Facts
      4. Summary
    2. 2. The Runtime Environment
      1. The ASP.NET Runtime Machinery
        1. ASP.NET and the IIS Web Server
          1. Handling the Request
          2. ISAPI Extensions
          3. ISAPI Filters
          4. Extensions and Filters in IIS 7.0
          5. Application Pools
          6. ASP.NET Runtime Components
        2. Life Cycle of an ASP.NET Request
          1. Events in the Request Life Cycle
          2. ASP.NET Request Processing in Classic Pipeline Mode
          3. ASP.NET Request Processing in Integrated Pipeline Mode
          4. What Executes the ASP.NET Request?
        3. What's an HTTP Handler, Anyway?
          1. The IHttpHandler Interface
          2. A Simple but Effective HTTP Handler
          3. Registering the HTTP Handler
          4. HTTP Handlers as ASHX Resources
          5. HTTP Handlers in an ASP.NET MVC Application
        4. What's an HTTP Module, Anyway?
          1. The IHttpModule Interface
          2. Wiring Up Events
          3. Registering HTTP Modules
        5. URL Routing
          1. Original URL Rewriting API
          2. URL Routing Engine
          3. URL Patterns and Routes
          4. Route Handlers
          5. Mapping URLs to Routes
          6. Handling Requests for Physical Files
          7. Preventing Routing for Defined URLs
      2. The ASP.NET MVC Run-Time Shell
        1. The Big Picture
          1. Default Configuration
          2. ASP.NET MVC and Web Server Compatibility
          3. Special Settings for HTTP Handlers and HTTP Modules
          4. Routing the Request
          5. Mixing Web Forms and MVC Pages
        2. Processing an ASP.NET MVC Request
          1. The MvcRouteHandler Class
          2. The MvcHandler Class
          3. ASP.NET MVC Wrapper Objects
      3. Summary
    3. 3. The MVC Pattern and Beyond
      1. The Original MVC Pattern
        1. MVC Interaction Model
        2. The Original Idea
        3. Presenting the Actors
          1. The View Actor on the Stage
          2. The Controller Actor on the Stage
        4. Limitations of the MVC Pattern
          1. Insufficient Testability
          2. Insufficiently Clean Design
      2. The Model2 Pattern
        1. MVC and the Web
          1. A Brief History of Model2
          2. The Controller Actor over the Web
          3. The View Actor over the Web
          4. The Model Actor over the Web
        2. Model2 and ASP.NET MVC
          1. Implementation of the Controller Actor
          2. Implementation of the View Actor
          3. Implementation of the Model Actor
      3. Presentation-Oriented Variations of MVC
        1. The MVP Pattern
          1. MVP Actors at a Glance
          2. Implementation of the View Actor
          3. Implementation of the Presenter Actor
          4. MVP and Enterprise-Class Applications
          5. Cardinality of MVP Triads
        2. Presentation Model Pattern (Also Known as MVVM)
          1. PM Actors at a Glance
          2. Implementation of the Model Actor
          3. Implementation of the View Actor
          4. Implementation of the Presenter Actor
          5. MVVM in Rich User Interfaces
      4. The ASP.NET MVC Project Template
        1. Peculiarities of an ASP.NET MVC Project
          1. The web.config File
          2. The global.asax File
          3. The default.aspx File
          4. Convention-over-Configuration
        2. ASP.NET MVC Special Folders
          1. The Controllers Folder
          2. The Views Folder
          3. The Areas Folder
          4. Other Folders in the Project
      5. Summary
  7. II. The Core of ASP.NET MVC
    1. 4. Inside Controllers
      1. The Role of Controllers and the Motivation for Using Them
        1. Beyond the Code-Behind Approach
          1. Limited Code Visibility and Control
          2. Limited Testability
          3. Tightly Coupled to Event Handlers
          4. Further Layering Is Up to You
        2. Introducing Controllers
          1. Controllers in ASP.NET Web Forms
          2. Views in ASP.NET Web Forms
          3. Web Forms Views and Controllers Are Mostly About Overhead
          4. Testing in ASP.NET Web Forms
        3. Mechanics of Controllers in ASP.NET MVC
          1. Processing HTTP Requests
          2. The Central Role of Controllers
          3. Actions and Controllers
          4. A Typical Controller Class
          5. Controller Methods and Input Parameters
      2. Anatomy of an ASP.NET MVC Controller
        1. Inside the Structure of a Controller
          1. The IController Interface
          2. The ControllerBase Class
          3. The Controller Class
          4. Execution of a Request
          5. Filter Interfaces for a Controller
        2. Behavior of a Controller
          1. Input Parameters of an Action Method
          2. Using Complex Data Types in an Action Method
          3. Action Methods
          4. Behavior of an Action Method
          5. Passing Data to a View
          6. The View-Model Container
          7. Return Value of an Action Method
        3. Attributes of Controllers and Action Methods
          1. Filter Attributes
          2. The Authorize Attribute
          3. The HandleError Attribute
          4. The OutputCache Attribute
          5. The ValidateAntiForgeryToken Attribute
          6. The ValidateInput Attribute
          7. Invocation Attributes
          8. Action Selector Attributes
      3. Writing a Controller
        1. Design of a Controller Class
          1. Mapping Functions to Controllers
          2. Mapping Behavior to Methods
          3. Action Methods and Stereotypes
          4. Action Methods: Coordinator vs. Controller
        2. Should You Use Your Own Base Class?
          1. Signature of a Controller SuperClass
          2. Centralized Exception Handling
          3. Handling Unknown Actions
          4. Managing Dependencies
          5. The Controller's Factory
      4. Special Capabilities
        1. Grouping Controllers
          1. The Rationale Behind Areas
          2. Defining Areas in Your Project
          3. Registering Routes to Areas
          4. Linking to Areas
        2. Asynchronous Controllers
          1. Mechanics of Asynchronous Actions
          2. The Async Point
          3. Async Actions in ASP.NET MVC
          4. Designing Asynchronous Action Methods
          5. Coding Asynchronous Action Methods
          6. Attributes of Asynchronous Action Methods
          7. Candidates for Asynchronous Actions
        3. Render Actions
          1. The RenderAction Helper
          2. Child Actions
          3. RenderAction vs. RenderPartial
      5. Controllers and Testability
        1. Making Controllers Easy to Test
          1. Basics of Testability
          2. Dealing with Dependencies
          3. Tightly Coupled Controllers
          4. ServiceLayerContext Class
          5. Propagating Testability Changes to the Service Layer
        2. Writing Unit Tests
          1. Basics of Unit Testing with MSTest
          2. Writing a Sample Unit Test
          3. From Fakes to Mocks
      6. Summary
    2. 5. Inside Views
      1. Views and Controllers
        1. From Controllers to Views
          1. Views and Action Results
          2. Passing Data to the View
        2. Building the Response for the Browser
          1. The View Engine
          2. A Template for the View
      2. Anatomy of an ASP.NET MVC View
        1. Selecting the View
          1. The Controller's View Method
          2. The Action Invoker
          3. The ViewResult Class
          4. The ViewContext Class
        2. Creating the View
          1. The View Engine
          2. The View Object
          3. Partial Views
        3. The Default View Engine
          1. The VirtualPathProvider View Engine
          2. Core Services of a Path-Based View Engine
          3. When the View Name Can't Be Resolved
        4. The Web Forms View Engine
          1. The WebFormsViewEngine Class
          2. Setting Location Formats
          3. The WebFormView Class
          4. The ViewPage and ViewUserControl Classes
          5. Filtering ViewData Content in Partial Views
          6. Accepting Only a Section of the ViewData Content
          7. Rendering the View
      3. Writing a View
        1. The View's Template
          1. Adding a New View
          2. Partial Views
          3. Master Pages
          4. Strongly Typed Views
          5. ViewData vs. Model
        2. Filling Up the View
          1. ASPX Markup
          2. Code Blocks
          3. Adding Logic to the View
          4. View and ASP.NET Intrinsics
          5. View and Configuration Settings
          6. Localizing a View
        3. HTML Helpers
          1. The HtmlHelper Class
          2. HTML Encoding
          3. Rendering HTML Forms
          4. Rendering Input Elements
          5. Action Links
        4. Templated HTML Helpers
          1. Flavors of a Templated Helper
          2. Editing Helpers in Action
          3. The Display Helper in Action
        5. Datagrids and Paged Views
          1. Using Server Controls
          2. Intricacies of the Postback Model in ASP.NET MVC 2
          3. Building a Grid of Data with HTML Helpers
          4. Adding Paging Support
          5. Adding AJAX Capabilities
        6. Testing a View
          1. Testing the HTML in the View
          2. Testing the Behavior of the View
          3. Testing Posted Data
      4. Summary
    3. 6. Inside Models
      1. What's the Model, Anyway?
        1. How Many Types of Models Do You Know?
          1. Data Posted to the Controller
          2. Data Worked On in the View
          3. Domain-Specific Entities
        2. The Models Folder
          1. One-Model-Fits-All: Approach or Antipattern?
          2. My Model Is the Domain Model
          3. Posting Data to Domain Objects
          4. Issues with Direct Domain Object Binding
          5. Passing Data to the View
      2. Domain Model and View-Model
        1. Business Object Modeling
          1. The Model and Persistence
          2. Abstracting Domain Entities to a Model
          3. The Active Record Pattern
        2. Adding Validation Logic to the Model
          1. A General Approach
          2. The Validation Application Block
          3. Validation in an Entity Framework Scenario
          4. Data Annotations
          5. Data Annotations and Entity Framework
        3. Data for the View
          1. Is ViewData Just for Dummies?
          2. Strongly Typed Views
          3. The View-Model
          4. View-Model Builders
          5. Common Data for the Common View
      3. Model Binding
        1. The Model Binder in Action
          1. The DefaultModelBinder Class
          2. Binding to Collection Types
          3. Customizing the Binding Process
          4. Custom Model Binders
          5. Implementing a Model Binder
          6. Registering a Custom Binder
      4. Summary
  8. III. Programming Features
    1. 7. Data Entry in ASP.NET MVC
      1. The Select-Edit-Save Pattern
        1. Presenting Data
          1. Handling Selection
          2. URL Formatting
        2. Editing Data
          1. Displaying an Input Forms
          2. Synchronizing the View and the URL
          3. The Post-Redirect-Get Pattern
        3. Saving Data
          1. Binding Input Data
          2. Validation
          3. Persistence
      2. Data Validation
        1. Validation on the Server Side
          1. Designing a Validation Layer with Enterprise Library
          2. Saving Business Rules to a Configuration File
          3. Checking the Validity of an Object
          4. The Model State
        2. Giving Feedback to the User
          1. Direct Rendering of the View
          2. The TempData Collection
          3. Persisting Validation Messages
          4. Redisplaying Attempted Values
          5. Temporary Messages
        3. Data Annotations and Validators
          1. Preliminary Considerations Regarding Data Annotations
          2. Metadata and Display
          3. Evolution of Model Binding in ASP.NET MVC 2
          4. Validating Annotated Objects
          5. Adding Custom Attributes
        4. Client-Side Validation
          1. Enabling Client Validation
          2. Validators for Custom Attributes
          3. A Word or Two About xVal
      3. Summary
    2. 8. The ASP.NET MVC Infrastructure
      1. Routing
        1. Dealing with Routes
          1. Processing a Route
          2. Route Handlers
          3. Using Route Constraints
          4. Testing Routes
        2. Keeping an Eye on SEO
          1. Devising Routes and URLs
          2. The Trailing Slash
          3. Same Content, Multiple URLs
          4. Permanent Redirection
      2. Error Handling
        1. Foundations of ASP.NET Error Handling
          1. Handling Program Exceptions
          2. Inside the HandleError Attribute
          3. Handling Route Exceptions
        2. Dealing with Missing Content
          1. Catch-All Route
          2. Skipping IIS Error-Handling Policies
      3. Localization
        1. Making Resources Localizable
          1. Overall Strategy for Global and Local Resources
          2. Dealing with Resources in ASP.NET
        2. Dealing with Resources in ASP.NET MVC
          1. Consuming Localized Resources
          2. Setting the Current Culture
          3. Changing Culture on the Fly
      4. Dependency Injection
        1. Dependency Inversion in Action
          1. The Service Locator Pattern
          2. The Dependency Injection Pattern
          3. Injection Mechanisms
          4. A Simple and Highly Testable Solution
          5. IoC Containers
          6. Advanced Features of IoC Containers
        2. A Brief Tour of Unity
          1. Registering Types and Instances
          2. Resolving Dependencies
          3. Declarative Configuration
          4. Lifetime Managers
        3. Creating a Global Container
          1. Custom Controller Factory
          2. Managing Dependencies
          3. Injecting a Custom Action Invoker
      5. Summary
    3. 9. AJAX Capabilities
      1. AJAX in ASP.NET
        1. Partial Rendering
          1. The Implementation
          2. The Good and the Bad
        2. Direct Scripting
          1. The Overall Idea
          2. Invoking the HTTP Façade
          3. The Good and the Bad
      2. AJAX in ASP.NET MVC
        1. The JavaScript API
          1. Using jQuery to Perform AJAX Calls
          2. Invoking Web Services from ASP.NET MVC
          3. The ASP.NET AJAX 4 Library
        2. The Controller Façade
          1. Returning JSON Content
          2. Returning JSONP Content
          3. Returning JavaScript Content
          4. Returning Markup
        3. AJAX Helpers in ASP.NET MVC
          1. The AjaxHelper Class
          2. Extension Methods for the AjaxHelper Class
          3. AJAX Action Links
          4. AJAX Options
          5. Dealing with the Client-Side Events
        4. Partial Rendering in ASP.NET MVC
          1. Updating the User Interface
          2. Posting Forms the AJAX Way
          3. Adjusting the URL on the Fly
          4. Reflecting on Partial Rendering in ASP.NET MVC
      3. Summary
    4. 10. Testability and Unit Testing
      1. Testability and Design
        1. Design for Testability
          1. The Attribute of Control
          2. The Attribute of Visibility
          3. The Attribute of Simplicity
        2. Loosen Up Your Design
          1. Interface-Based Programming
          2. Dependency Injection
          3. Relativity of Software Testability
          4. Testability and Coupling
          5. Testability and Object Orientation
      2. Basics of Unit Testing
        1. Working with a Test Harness
          1. MSTest and NUnit
          2. Text Fixtures
          3. Arrange, Act, Assert
          4. Data-Driven Tests
        2. Aspects of Testing
          1. Very Limited Scope
          2. Testing in Isolation
          3. Fakes and Mocks
          4. Assertions per Test
          5. Testing Inner Members
          6. Code Coverage
      3. Unit Testing in ASP.NET MVC
        1. Testing Controller Actions
          1. Testing Controller Actions
          2. Passing Parameters to a Controller Action
          3. Testing Different Views
          4. Testing Redirections
        2. Injecting Mocks and Fakes
          1. Ignoring Dependencies
          2. Interacting with Dependencies
        3. Mocking the HTTP Context
          1. Mocking the Session State
          2. Building a Fake HTTP Context
          3. Mocking the Request Object
          4. Mocking the Response Object
          5. Mocking the ASP.NET Cache
        4. More Specific Tests
          1. Testing HTML Helpers
          2. Testing Localized Resources
          3. Testing Asynchronous Methods
      4. Summary
    5. 11. Customizing ASP.NET MVC
      1. The Controller Factory
        1. ASP.NET MVC Request Processing
          1. Inside the MvcHandler Class
          2. The Controller Builder
          3. The Default Controller Factory
        2. Extending the Default Controller Factory
          1. Getting the Controller Type
          2. The Controller Type Cache
          3. Customizing the Controller Name
          4. Getting the Controller Instance
          5. Releasing the Controller Instance
        3. Invoking Actions
          1. Role of the Action Invoker
          2. The Default Action Invoker
          3. Controller Descriptors
          4. The Controller Descriptor Cache
          5. Action Descriptors
          6. Custom Invokers
      2. Action Filters
        1. Gallery of Action Filters
          1. Browser-Specific Views
          2. Linking Data Shared Across Views
          3. Compressing the Response
        2. Loading Action Filters Dynamically
          1. Interception Points for Filters
          2. Adding an Action Filter Using Fluent Code
          3. Building Up Filters via an IoC Container
        3. Action Selectors
          1. Selecting an Action by Name
          2. Action Method Selectors
      3. Action Results and Rendering
        1. Processing the Result of the Action
          1. Generating the Response for the Browser
          2. Dissecting Some Built-in Action Result Classes
        2. Custom ActionResult Objects
          1. The PermanentRedirectResult Object
          2. A Syndication Result Object
          3. Returning Binary Data
          4. Inside Built-in Binary Action Result Classes
        3. View Engines
          1. Adding an Alternate View Engine
          2. Overview of the Spark View Engine
          3. Defining a Layout with Spark
          4. Importing the View Model with Spark
        4. HTML Helpers
          1. ASP.NET MVC and a Component Model
          2. Emitting Common HTML Elements
          3. Emitting Common HTML Blocks
          4. Emitting Common Blocks of jQuery UI Script
      4. Summary
  9. A. ReSharper and the Power of Tools
    1. IntelliSense Extensions
      1. Choose the Right View Name with IntelliSense Tips
      2. Action Links and URLs
      3. User Controls
    2. Static Analysis to Detect Missing Views and Actions
    3. Coding Assistants
      1. Creating Views by Usage
        1. Creating Action Links by Usage
        2. Surround with Tags
    4. Navigation
      1. Controller and View Navigation
      2. Locating Symbols
      3. Navigating Inside Master and Content Pages
      4. Refactoring for ASP.NET MVC
    5. Conclusion
  10. Index
  11. About the Author
  12. Copyright