HAPPY BOOKSGIVING
Use code BOOKSGIVING during checkout to save 40%-55% on books and eBooks. Shop now.
Register your product to gain access to bonus material or receive a coupon.
In this book, world-renowned ASP.NET expert and member of the Microsoft ASP.NET team Stephen Walther shows experienced developers how to use Microsoft’s new ASP.NET MVC Framework to build web applications that are more powerful, flexible, testable, manageable, scalable, and extensible.
Writing for professional programmers, Walther explains the crucial concepts that make the Model-View-Controller (MVC) development paradigm work so well and shows exactly how to apply them with the ASP.NET MVC Framework. From controllers and actions to views and models, Walther demonstrates how to apply each ASP.NET MVC Framework feature in real-world projects.
In Part II, you’ll walk step-by-step through building a full-fledged ASP.NET MVC blog application that implements capabilities ranging from data access to validation. Through this case study, you’ll learn how to build ASP.NET MVC applications using test-driven development processes that enable rapid feedback, greater productivity, and better quality.
Throughout, Walther presents extensive code examples, reflecting his unsurpassed experience as an ASP.NET instructor, a leading commercial developer, and now as a member of Microsoft’s ASP.NET development team.
Understand how to:
Download the book files in one zip file
Download the Chapter 2 book files
Download the Chapter 3 book files
Download the Chapter 4 book files
Download the Chapter 5 book files
Download the Chapter 6 book files
Download the Chapter 7 book files
Download the Chapter 8 book files
Download the Chapter 9 book files
Download the Chapter 10 book files
Download the Chapter 11 book files
Download the Chapter 12 book files
Download the Chapter 13 book files
Download the Chapter 14 book files
Download the Chapter 15 book files
Download the Chapter 17 book files
Download the Chapter 18 book files
Download the Chapter 19 book files
Download the Chapter 20 book files
Download the Chapter 21 book files
Download the Common Code files
Beyond HTML: Returning JSON and XML Data From Your MVC Endpoints
Get Started Using ASP.NET MVC for Your .NET Applications
An Introduction to ASP.NET MVC
Download the sample pages (includes Chapter 1 and Index)
Introduction 1
How This Book Is Organized 1
What You Should Know Before Reading This Book 2
What Software Do You Need? 2
Where Do You Download the Code Samples? 3
If You Like This Book 3
Part I Building ASP.NET MVC Applications
Chapter 1 An Introduction to ASP.NET MVC 7
A Story with a Moral 7
What Is Good Software? 8
Avoiding Code Smells 9
Software Design Principles 10
Software Design Patterns 11
Writing Unit Tests for Your Code 12
Test-Driven Development 13
Short-Term Pain, Long-Term Gain 14
What Is ASP.NET MVC? 14
ASP.NET MVC Is Part of the ASP.NET Framework 14
The Origins of MVC 15
The Architecture of an ASP.NET MVC Application 16
Understanding the Sample ASP.NET MVC Application 17
ASP.NET MVC Folder Conventions 19
Running the Sample ASP.NET MVC Application 19
Chapter 2 Building a Simple ASP.NET MVC Application 23
Starting with a Blank Slate 23
Creating the Database 25
Creating the Model 27
Creating the Controller 30
Creating the Views 37
Adding the Index View 38
Adding the Create View 42
Chapter 3 Understanding Controllers and Actions 47
Creating a Controller 47
Returning Action Results 51
Returning a View Result 52
Returning a Redirect Result 55
Returning a Content Result 57
Returning a JSON Result 59
Returning a File Result 63
Controlling How Actions Are Invoked 65
Using AcceptVerbs 65
Using ActionName 70
Using ActionMethodSelector 72
Handling Unknown Actions 76
Testing Controllers and Actions 78
Chapter 4 Understanding Views 83
Creating a View 83
Using View Data 87
Typed and Untyped Views 88
Creating Strongly Typed Views 94
Preventing JavaScript Injection Attacks 95
Using Alternative View Engines 97
Creating a Custom View Engine 99
Testing Views 105
Test the View Result 105
Test HTML Helpers 108
Test a Custom View Engine 114
Chapter 5 Understanding Models 119
Creating a Data Model 120
Creating a Data Model with the Microsoft Entity Framework 120
Listing Records 124
Getting a Single Record 126
Creating Records 127
Editing Records 128
Deleting Records 131
Using the Repository Pattern 132
Creating a Product Repository 133
Using the Dependency Injection Pattern 138
Creating a Generic Repository 139
Using the Generic Repository with the Entity Framework 141
Using the Generic Repository with LINQ to SQL 144
Extending the Generic Repository 147
Testing Data Access 149
Testing with a Mock Repository 150
Testing with a Fake Generic Repository 155
Chapter 6 Understanding HTML Helpers 159
Using the Standard HTML Helpers 160
Rendering Links 160
Rendering Image Links 161
Rendering Form Elements 162
Rendering a Form 166
Rendering a Drop-Down List 167
Encoding HTML Content 169
Using Antiforgery Tokens 169
Creating Custom HTML Helpers 173
Using the TagBuilder Class 176
Using the HtmlTextWriter Class 180
Creating a DataGrid Helper 183
Adding Sorting to the DataGrid Helper 190
Adding Paging to the DataGrid Helper 192
Testing Helpers 201
Chapter 7 Understanding Model Binders and Action Filters 207
Understanding Model Binders 207
Using the Default Model Binder 210
Binding to Complex Classes 212
Using the Bind Attribute 218
Using Bind with Classes 221
Using Prefixes When Binding 225
Using the Form Collection Model Binder 228
Using the HTTP Posted File Base Model Binder 231
Creating a Custom Model Binder 233
Understanding Action Filters 236
Creating a Log Action Filter 237
Chapter 8 Validating Form Data 241
Understanding Model State 241
Understanding the Validation Helpers 245
Styling Validation Error Messages 247
Prebinding and Postbinding Validation 248
Validating with a Service Layer 251
Validating with the IDataErrorInfo Interface 258
Testing Validation 264
Chapter 9 Understanding Routing 269
Using the Default Route 269
Debugging Routes 274
Creating Custom Routes 275
Creating Route Constraints 277
Using Regular Expression Constraints 278
Using the HttpMethod Constraint 280
Creating an Authenticated Constraint 280
Creating a NotEqual Constraint 283
Using Catch-All Routes 285
Testing Routes 288
Using the MvcFakes and RouteDebugger Assemblies 289
Testing If a URL Matches a Route 289
Testing Routes with Constraints 292
Chapter 10 Understanding View Master Pages and View User Controls 295
Understanding View Master Pages 295
Creating a View Master Page 295
Creating a View Content Page 300
Setting the Master Page from the Controller 302
Setting the Master Page Title 303
Nested Master Pages 306
Passing View Data to Master Pages 308
Understanding View User Controls 311
Passing View Data to User Controls 314
Using a View User Control as a Template 319
Chapter 11 Better Performance with Caching 325
Using the OutputCache Attribute 325
Don’t Cache Private Data 330
What Gets Cached? 331
Setting the Cache Location 333
Varying the Output Cache by Parameter 335
Varying the Output Cache 337
Removing Items from the Output Cache 341
Using Cache Profiles 343
Using the Cache API 344
Using the HttpCachePolicy Class 345
Using the Cache Class 347
Testing the Cache 353
Testing the OutputCache Attribute 353
Testing Adding Data to the Cache 355
Chapter 12 Authenticating Users 365
Creating Users and Roles 365
Using the Web Site Administration Tool 365
Using the Account Controller 367
Authorizing Users 368
Using the Authorize Attribute 368
Using the User Property 372
Configuring Membership 374
Configuring the Membership Database 375
Configuring Membership Settings 378
Using the Membership and Role Manager API 381
Using Windows Authentication 385
Configuring Windows Authentication 385
Authenticating Windows Users and Groups 386
Testing Authorization 390
Testing for the Authorize Attribute 390
Testing with the User Model Binder 393
Chapter 13 Deploying ASP.NET MVC Applications 401
Configuring IIS for ASP.NET MVC 401
Integrated Versus Classic Mode 402
Using ASP.NET MVC with Older Versions of IIS 403
Adding Extensions to the Route Table 403
Hosted Server 408
Creating a Wildcard Script Map 410
Mixing ASP.NET Web Forms and ASP.NET MVC 414
Modifying the Visual Studio Project File 415
Adding the Required Assemblies 415
Modifying the Web Configuration File 416
Modify the Global.asax File 422
Using Web Forms and MVC 424
Bin Deploying an ASP.NET MVC Application 424
Chapter 14 Working with Ajax 427
Using the Ajax Helpers 427
Debugging Ajax 428
Posting a Form Asynchronously 430
Displaying Progress 435
Updating Content After Posting 443
Performing Validation 447
Providing Downlevel Browser Support 452
Retrieving Content Asynchronously 454
Highlighting the Selected Link 459
Creating a Delete Link 462
Providing Downlevel Browser Support 468
Using the AcceptAjax Attribute 473
Chapter 15 Using jQuery 479
Overview of jQuery 479
Including the jQuery Library 480
jQuery and Visual Studio Intellisense 481
Using jQuery Selectors 482
Adding Event Handlers 487
Using jQuery Animations 489
jQuery and Ajax 491
Using jQuery Plug-Ins 498
Part II Walkthrough: Building the Unleashed Blog Application
Chapter 16 Overview of the Application 505
What Is Test-Driven Development? 505
Why Do Test-Driven Development? 506
The KISS and YAGNI Principles 507
Waterfall Versus Evolutionary Design 507
TDD Tests Are Not Unit Tests 508
Tests Flow from User Stories 508
Unit Testing Frameworks 509
Bibliography of Test-Driven Development 509
Chapter 17 Database Access 511
Creating the Unleashed Blog Project 511
Creating Our First Test 514
Creating New Blog Entries 520
Refactoring to Use the Repository Pattern 524
Creating a Fake Blog Repository 526
Creating an Entity Framework Repository 530
Creating the Database Objects 531
Creating the Entity Framework Data Model 532
Creating the Entity Framework Blog Repository 534
Using the Entity Framework Repository 537
Chapter 18 Creating the Routes 543
Creating the Controller Tests 543
Creating the Route Tests 553
Creating the Archive Routes 561
Trying Out the Archive Controller 564
Chapter 19 Adding Validation 567
Performing Validation in the Simplest Possible Way 567
Refactoring the Test Code 573
Validating the Length of a Property 576
A Web Browser Sanity Check 578
Refactoring to Use a Service Layer 581
Adding Business Rules 586
Chapter 20 Paging, Views, and Ajax 593
Adding Paging Support 593
Adding the Views 605
Adding Ajax Support 612
Chapter 21 Adding Comments 621
Implementing Comments 621
Adding Comments to the Database 633
Displaying Comments and Comment Counts 637
Part III Appendixes
Appendix A C# and VB.NET Language Features 647
Type Inference 647
Object Initializers 648
Anonymous Types 649
Nullable Types 651
Extension Methods 652
Generics 654
Lambda Expressions 655
LINQ 656
Appendix B Using a Unit Testing Framework 659
Using Visual Studio Unit Test 660
Understanding the Test Attributes 666
Using Assertions 669
Running the Tests 669
Limiting Visual Studio Test Results 671
Using NUnit 672
Creating an NUnit Unit Test Project 672
Creating a Test 674
Running Tests 676
Appendix C Using a Mock Object Framework 679
Understanding the Terminology 680
Installing Moq 680
Using Moq to Create a Class from an Interface 681
Returning Fake Values 690