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