Programming Microsoft® ASP.NET MVC

Book description

Fully updated for ASP.NET MVC 3. Delve into the features, principles, and pillars of the ASP.NET MVC framework—deftly guided by web development luminary Dino Esposito. ASP.NET MVC forces developers to think in terms of distinct components—Model, View, Controller—that make it easier to manage application complexity, while allowing strict control over the markup. Plunge into the framework’s internal mechanics and gain perspectives on how to use this programming model versus Web Forms, and begin building your own MVC-based applications quickly.

Table of contents

  1. Dedication
  2. A Note Regarding Supplemental Files
  3. Introduction
    1. Who Should Read This Book
      1. Assumptions
    2. Who Should Not Read This Book
    3. System Requirements
    4. Code Samples
    5. Errata & Book Support
    6. We Want to Hear from You
    7. Stay in Touch
  4. Acknowledgments
  5. I. ASP.NET MVC Fundamentals
    1. 1. ASP.NET MVC Controllers
      1. Routing Incoming Requests
        1. Simulating the ASP.NET MVC Runtime
          1. Defining the Syntax of Recognized URLs
          2. Defining the Behavior of the HTTP Handler
          3. Invoking the HTTP Handler
        2. The URL Routing HTTP Module
          1. Superseding URL Rewriting
          2. Routing the Requests
          3. Internal Structure of the URL Routing Module
        3. Application Routes
          1. URL Patterns and Routes
          2. Defining Application Routes
          3. Processing Routes
          4. Route Handler
          5. Handling Requests for Physical Files
          6. Preventing Routing for Defined URLs
      2. The Controller Class
        1. Aspects of a Controller
          1. Granularity of Controllers
          2. Stateless Components
          3. Further Layering Is Up to You
          4. Highly Testable
        2. Writing Controller Classes
          1. From Routing to Controllers
          2. From Routing to Actions
          3. Actions and HTTP Verbs
          4. Action Methods
        3. Processing Input Data
          1. Getting Input Data from the Request Object
          2. Getting Input Data from the Route
          3. Getting Input Data from Multiple Sources
          4. The ValueProvider Dictionary
        4. Producing Action Results
          1. Inside the ActionResult Class
          2. Predefined Action Result Types
          3. Mechanics of Executing Action Results
          4. Returning HTML Markup
          5. Returning JSON Content
      3. Special Capabilities of Controllers
        1. Grouping Controllers
          1. The Rationale Behind Areas
          2. Defining Areas in Your Project
          3. Adding Area Information to Routes
          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
      4. Summary
    2. 2. ASP.NET MVC Views
      1. Structure and Behavior of a View Engine
        1. Mechanics of a View Engine
          1. Detecting Registered View Engines
          2. Anatomy of a View Engine
          3. Who Calls the View Engine?
          4. The View Object
        2. Definition of the View Template
          1. Resolving the Template
          2. Default Conventions and Folders
          3. The Template for the View
          4. The Master View
      2. HTML Helpers
        1. Basic Helpers
          1. Rendering HTML Forms
          2. Rendering Input Elements
          3. Action Links
          4. Partial Views
          5. The HtmlHelper Class
        2. Templated Helpers
          1. Flavors of a Templated Helper
          2. The Display Helpers
          3. The Editor Helpers
        3. Custom Helpers
          1. Structure of an HTML Helper
          2. MvcHtmlString Is Better Than Just a String
          3. A Sample HTML Helper
          4. A Sample Ajax Helper
      3. The Web Forms View Engine
        1. Inside the View Engine
          1. Search Locations
          2. Search Location Formats
          3. Virtual Path Providers
        2. Designing a Sample View
          1. Defining a Master View
          2. Code Blocks
          3. HTML Helpers
          4. Server Controls
          5. View and ASP.NET Intrinsics
      4. The Razor View Engine
        1. Inside the View Engine
          1. Search Locations
          2. Code Nuggets
          3. Special Expressions of Code Nuggets
          4. The Razor Syntax and Visual Basic
          5. The Razor View Object
          6. Deriving from a Custom Base Class
        2. Designing a Sample View
          1. Defining the Model for the View
          2. Defining a Master View
          3. Defining Sections
          4. Default Content for Sections
          5. Nested Layouts
          6. Declarative HTML Helpers
          7. Limitations of Declarative HTML Helpers
        3. Templated Delegates
          1. Templated Delegates and Formatted Text
          2. Templated Delegates and Optional Sections
          3. The ViewStart Page Class
      5. Coding the View
        1. Modeling the View
          1. The ViewData Dictionary
          2. The ViewBag Dictionary
          3. Strongly Typed View Models
          4. Using Expando Objects to Control Class Proliferation
          5. Packaging the View-Model Classes
        2. Advanced Features
          1. Custom View Engines
          2. Render Actions
          3. Child Actions
          4. Testing the View
      6. Summary
    3. 3. The Model-Binding Architecture
      1. The Input Model
        1. Evolving from the Web Forms Input Processing
          1. Role of Server Controls
          2. Role of the View State
        2. Input Processing in ASP.NET MVC
          1. Role of Model Binders
          2. Flavors of a Model
      2. Model Binding
        1. Model-Binding Infrastructure
          1. Analyzing the Method’s Signature
          2. Getting the Binder for the Type
        2. The Default Model Binder
          1. Binding Primitive Types
          2. Dealing with Optional Values
          3. Value Providers and Precedence
          4. Binding Complex Types
          5. Binding Collections
          6. Binding Collections of Complex Types
          7. Binding Content from Uploaded Files
        3. Customizable Aspects of the Default Binder
          1. The Bind Attribute
          2. Creating Whitelists of Properties
          3. Creating Blacklists of Properties
          4. Using a Prefix
      3. Advanced Model Binding
        1. Custom Type Binders
          1. Customizing the Default Binder
          2. Implementing a Model Binder from Scratch
          3. Registering a Custom Binder
        2. A Sample DateTime Model Binder
          1. The Displayed Data
          2. The Controller Method
          3. Creating the DateTime Binder
      4. Summary
    4. 4. Input Forms
      1. General Patterns of Data Entry
        1. A Classic Select-Edit-Post Scenario
          1. Presenting Data and Handling the Selection
          2. Editing Data
          3. Saving Data
        2. Applying the Post-Redirect-Get Pattern
          1. Keeping the URL and Content in Sync
          2. Splitting POST and GET Actions
          3. Updating Only via POST
          4. Saving Temporary Data Across Redirects
        3. Ajax-Based Forms
          1. Creating an Ajax Form
          2. Infrastructure for Ajax-Based Forms
          3. Ajax-Aware Controller Methods
          4. Updating the Page Title After an Ajax Request
          5. Unobtrusive JavaScript Code
          6. Troubles with Unobtrusive Ajax
          7. Ajax Hyperlinks
      2. Automating the Writing of Input Forms
        1. Predefined Display and Editor Templates
          1. Annotating Data Members for Display
          2. Default Templates for Data Types
          3. Custom Templates for Data Types
          4. Data Type Validation
          5. Read-Only Members
          6. Custom Templates in Razor
        2. Custom Templates for Model Data Types
          1. Tabular Templates
          2. Dealing with Nested Models
      3. Input Validation
        1. Using Data Annotations
          1. Validation Provider Infrastructure
          2. Decorating a Model Class
          3. Dealing with Enumerated Types
          4. Controlling Error Messages
        2. Advanced Data Annotations
          1. Cross-Property Validation
          2. Creating Custom Validation Attributes
          3. Enabling Client-Side Validation
          4. Culture-Based, Client-Side Validation
          5. Validating Properties Remotely
        3. Self-Validation
          1. The IValidatableObject Interface
          2. Benefits of Centralized Validation
          3. The IClientValidatable Interface
          4. Dynamic Server-Side Validation
      4. Summary
  6. II. ASP.NET MVC Software Design
    1. 5. Aspects of ASP.NET MVC Applications
      1. ASP.NET Intrinsic Objects
        1. SEO and HTTP Response
          1. Permanent Redirection
          2. Devising Routes and URLs
          3. The Trailing Slash
        2. Managing the Session State
          1. Using the Session Object
          2. Never Outside the Controller
        3. Caching Data
          1. The Bright Side and Dark Side of the Native Cache Object
          2. Injecting a Caching Service
          3. A Better Way of Injecting a Caching Service
          4. Distributed Caching
          5. Caching the Method Response
          6. Partial Output Caching
      2. Error Handling
        1. Handling Program Exceptions
          1. Handling Exceptions Directly
          2. Overriding the OnException Method
          3. Using the HandleError Attribute
        2. Global Error Handling
          1. Global Error Handling from global.asax
          2. Global Error Handling Using an HTTP Module
          3. Intercepting Model-Binding Exceptions
          4. Handling Route Exceptions
        3. Dealing with Missing Content
          1. Catch-All Route
          2. Skipping IIS Error-Handling Policies
      3. Localization
        1. Using Localizable Resources
          1. No Global, No Local—Just Resources
          2. Localizable Text
          3. Localizable Files
          4. Referencing Embedded Files
          5. Localizable Views
        2. Dealing with Localizable Applications
          1. Auto-Adapting Applications
          2. Multilingual Applications
          3. Changing Culture Programmatically
          4. Storing Localized Resources in a Database
      4. Summary
    2. 6. Securing Your Application
      1. Security in ASP.NET MVC
        1. Authentication and Authorization
          1. Configuring Authentication in ASP.NET MVC
          2. Restricting Access to Action Methods
          3. Handling Authorization for Action Methods
          4. Authorization and Output Caching
        2. Extending the Authorize Attribute
          1. Not Logged In or Not Authorized?
          2. Hiding Critical User Interface Elements
      2. Implementing a Membership System
        1. Defining a Membership Controller
          1. Validating User Credentials
          2. Integrating with the Membership API
          3. Integrating with the Role API
        2. The Remember-Me Feature and Ajax
          1. Reproducing the Problem
          2. Solving the Problem
      3. External Authentication Services
        1. The OpenID Protocol
          1. Identifying Users Through an OpenID Provider
          2. OpenID vs. OAuth
        2. Authenticating via Twitter
          1. Registering Your Application
          2. Starting the Authentication Process
          3. Dealing with the Twitter Response
          4. The Token Manager
      4. Summary
    3. 7. Design Considerations for ASP.NET MVC Controllers
      1. Shaping Up Your Controller
        1. Choosing the Right Stereotype
          1. RDD at a Glance
          2. Breaking Down the Execution of a Request
          3. Acting as a “Controller”
          4. Acting as a “Coordinator”
        2. Fat-Free Controllers
          1. Short Is Always Better
          2. Action Methods Coded as View Model Builders
          3. Worker Services
          4. Implementing a Worker Service
          5. Do We Really Need Controllers?
          6. The Ideal Action Method Code
      2. Connecting the Presentation and Back End
        1. The iPODD Pattern
          1. Beyond Classic Layers
          2. The (Idiomatic) Presentation Layer
          3. The Orchestration Layer
          4. The Domain Layer
          5. Exposing Entities of the Domain
          6. The Data Layer
        2. Injecting Data and Services in Layers
          1. The Dependency Inversion Principle
          2. The Service Locator Pattern
          3. The Dependency Injection Pattern
          4. Using Tools for Inversion-of-Control
          5. Poor Man’s Dependency Injection
        3. Gaining Control of the Controller Factory
          1. Registering a Custom Controller Factory
          2. Building a Customer Controller Factory
          3. A Unity-Based Controller Factory
      3. Summary
    4. 8. Customizing ASP.NET MVC Controllers
      1. The Extensibility Model of ASP.NET MVC
        1. The Provider-Based Model
          1. Gallery of Extensibility Points
          2. Realistic Scenarios: Culture-Driven View Engine
          3. Realistic Scenarios: Alternate TempData Storage
          4. Using Custom Components in Your Applications
        2. The Service Locator Model
          1. Service Locator vs. Dependency Injection
          2. Service Locator in ASP.NET MVC
          3. Defining Your Dependency Resolver
      2. Adding Aspects to Controllers
        1. Action Filters
          1. Embedded and External Filters
          2. Classification of Action Filters
          3. Built-in Action Filters
          4. Global Filters
        2. Gallery of Action Filters
          1. Adding a Response Header
          2. Compressing the Response
          3. View Selector
          4. Filling Up the View Model
        3. Special Filters
          1. Action Name Selectors
          2. Action Method Selectors
          3. Restricting a Method to Ajax Calls Only
          4. Restricting a Method to a Given Submit Button
        4. Building a Dynamic Loader Filter
          1. Interception Points for Filters
          2. Adding an Action Filter Using Fluent Code
          3. Customizing the Action Invoker
          4. Registering the Custom Invoker
          5. Enabling Dynamic Loading via a Filter Provider
      3. Action Result Types
        1. Built-in Action Result Types
          1. Returning a Custom Status Code
          2. Returning JavaScript Code
          3. Returning JSON Data
        2. Custom Result Types
          1. Returning JSONP Strings
          2. Returning Syndication Feed
          3. Dealing with Binary Content
          4. Returning PDF Files
      4. Summary
    5. 9. Testing and Testability in ASP.NET MVC
      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. Relativity of Software Testability
          3. Testability and Coupling
          4. Testability and Object Orientation
      2. Basics of Unit Testing
        1. Working with a Test Harness
          1. Choosing a Test Environment
          2. Test 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. Number of Assertions per Test
          5. Testing Inner Members
          6. Code Coverage
      3. Testing Your ASP.NET MVC Code
        1. Which Part of Your Code Should You Test?
          1. How Do I Find Relevant Code to Test?
          2. The Domain Layer
          3. The Orchestration Layer
          4. The Data Access Layer
        2. Unit Testing ASP.NET MVC Code
          1. Testing If the Returned View Is Correct
          2. Testing Localization
          3. Testing Redirections
          4. Testing Routes
        3. Dealing with Dependencies
          1. About Mock and Fake Objects
          2. Testing Code that Performs Data Access
          3. Testing Asynchronous Methods
          4. Refactoring to the Humble Object Pattern
        4. Mocking the HTTP Context
          1. Mocking the HttpContext Object
          2. Mocking the Request Object
          3. Mocking the Response Object
          4. Mocking the Session Object
          5. Mocking the Cache Object
          6. Testing Controller Methods with Filters
      4. Summary
  7. III. Client-Side
    1. 10. More Effective JavaScript
      1. Revisiting the JavaScript Language
        1. Language Basics
          1. Type System
          2. Null vs. Undefined
          3. Local and Global Variables
          4. Variables and Hoisting
          5. Objects
          6. Functions
        2. Object-Orientation in JavaScript
          1. Making Objects Look Like Classes
          2. Using Closures
          3. Using Prototypes
          4. Plain Custom Objects vs. a Hierarchy of Classes
      2. jQuery’s Executive Summary
        1. DOM Queries and Wrapped Sets
          1. The Root Object
          2. Running a Query
          3. Enumerating the Content of a Wrapped Set
        2. Selectors
          1. Basic Selectors
          2. Compound Selectors
          3. Predefined Filters
          4. Filter vs. Find
          5. Chaining Operations on a Wrapped Set
        3. Events
          1. Binding and Unbinding
          2. Live Event Binding
          3. Page and DOM Readiness
      3. Aspects of JavaScript Programming
        1. Unobtrusive Code
          1. Style the View with Code
          2. Pragmatic Rules of Unobtrusive JavaScript
          3. Using jQuery Plug-ins to Increase Unobtrusiveness
        2. Reusable Packages and Dependencies
          1. The Namespace Pattern
          2. The Module Pattern
        3. Script and Resource Loading
          1. The Download Is Always Synchronous
          2. Scripts at the Bottom
          3. Nonblocking Scripts Download
          4. Dealing with Static Files
          5. Using Sprites
      4. ASP.NET MVC, Ajax and JavaScript
        1. The Ajax Service Layer
          1. A Tailor-Made HTTP Façade
          2. ASP.NET MVC Controllers vs. Web Services
          3. Avoid Exposing WCF Services to Ajax Clients
        2. Ways to Write Ajax ASP.NET MVC Applications
          1. Using Ajax Helpers
          2. Intricacies of Ajax Helpers
          3. Using JSON Data Providers
          4. Organizing JavaScript Code (and Reorganizing Views)
          5. A Look at JavaScript Updaters
          6. Intricacies of JSON-Based Solutions
      5. Summary
  8. A. About the Author
  9. Index
  10. About the Author
  11. Copyright

Product information

  • Title: Programming Microsoft® ASP.NET MVC
  • Author(s): Dino Esposito
  • Release date: October 2011
  • Publisher(s): Microsoft Press
  • ISBN: 9780735665644