Skip to main content

Professional ASP.NET MVC 2

Professional ASP.NET MVC 2

Jon Galloway, Scott Hanselman, Phil Haack, Scott Guthrie, Rob Conery

ISBN: 978-0-470-90861-7

Jun 2010

552 pages

Select type: E-Book

Product not available for purchase

Description

Top-selling MVC book from a top team at Microsoft—now fully updated!

ASP.NET MVC 2.0 is now available and shipping with Visual Studio 2010 and .NET 4. A new update to Microsoft’s Model-View-Controller technologies, MVC 2 enables developers to build dynamic, data-driven Web sites. Professional ASP.NET MVC 2 shows you step-by-step how to use MVC 2. You’ll learn both the theory behind MVC 2, as well as walk through practical tutorials, where you’ll create a real-world application. Topics include transitioning from ASP.NET development, as well as an overview of related tools and technologies, including LINQ, jQuery, and REST.

This book is for web developers who are looking to add more complete testing to their web sites, and who are perhaps ready for "something different."

In some places, we assume that you're somewhat familiar with ASP.NET WebForms, at least peripherally. There are a lot of ASP.NET WebForms developers out there who are interested in ASP.NET MVC so there are a number of places in this book where we contrast the two technologies. Even if you're not already an ASP.NET developer, you might still find these sections interesting for context, as well as for your own edification as ASP.NET MVC may not be the web technology that you're looking for.

FOREWORD xxx

INTRODUCTION xxvii

CHAPTER 1: NERDDINNER 1

File ➪ New Project 6

Creating the Database 14

Building the Model 21

Controllers and Views 37

Create, Update, Delete Form Scenarios 57

ViewData and ViewModel 85

Partials and Master Pages 91

Paging Support 100

Authentication and Authorization 109

AJAX Enabling RSVPs Accepts 118

Integrating an AJAX Map 127

Unit Testing 146

NerdDinner Wrap-Up 165

CHAPTER 2: MODEL-VIEW-CONTROLLER AND ASP.NET 167

What Is Model-View-Controller? 167

MVC on the Web Today 169

ASP.NET MVC: The New Kid on the Block 173

Summary 177

CHAPTER 3: ASP.NET > ASP.NET MVC 179

Abstraction: What Web Forms Does Well 179

The Leak: Where Web Forms Doesn’t Exactly Fit 185

Back to Basics: ASP.NET MVC Believes… 188

Caring about Testability 190

Common Reactions to ASP.NET MVC 192

Why “(ASP.NET > ASP.NET MVC) == True” 193

Summary 201

CHAPTER 4: ROUTES AND URLS 203

Introduction to Routing 204

Under the Hood: How Routes Tie Your URL to an Action 226

Advanced Routing with Custom Constraints 227

Route Extensibility 228

Using Routing with Web Forms 235

Summary 241

CHAPTER 5: CONTROLLERS 243

History of the Controller 243

Defining the Controller: The IController Interface 245

The ControllerBase Abstract Base Class 247

The Controller Class and Actions 248

The ActionResult 252

Action Invoker 260

Summary 278

CHAPTER 6: VIEWS 279

What a View Does 279

What a View Shouldn’t Do 281

Specifying a View 281

Strongly Typed Views 283

ViewModels 284

HTML Helper Methods 285

The View Engine 299

New View Engine or New ActionResult? 309

Summary 309

CHAPTER 7: AJAX 311

When Ajax Is Cool 312

When It’s Not 313

Ajax Examples 314

Summary 339

CHAPTER 8: FILTERS 341

Filters Included with ASP.NET MVC 341

Writing a Custom Action Filter 354

Writing a Custom Authorization Filter 355

Writing a Custom Exception Filter 357

Filter Ordering 358

Filter Naming 359

Summary 361

CHAPTER 9: SECURING YOUR APPLICATION 363

This Is a War 365

Weapons 369

Threat: Cross-Site Scripting (XSS) 372

Threat: Cross-Site Request Forgery 381

Threat: Cookie Stealing 386

Threat: Over-Posting 387

Keeping Your Pants Up: Proper Error Reporting and the Stack Trace 389

Securing Your Controllers, Not Your Routes 390

Summary: It’s Up to You 392

CHAPTER 10: TEST DRIVEN DEVELOPMENT WITH ASP.NET MVC 395

A Brief Introduction to TDD 396

Applying TDD to ASP.NET MVC 404

Summary 412

CHAPTER 11: TESTABLE DESIGN PATTERNS 413

Why You Should Care About Testability 413

You Want to Write Testable Code 416

Using Tests to Prove You’re Done 417

Designing Your Application for Testability 417

Testable Data Access 422

Implementing Business Logic with the Service Layer 429

Summary 438

CHAPTER 12: BEST OF BOTH WORLDS: WEB FORMS AND MVC TOGETHER 441

How Is It Possible? 442

Including MVC in Existing Web Forms Applications 442

Adding Web Forms to an Existing ASP.NET MVC Application 448

Sharing Data between Web Forms and MVC 451

Migrating from Web Forms to MVC 456

Summary 469

CHAPTER 13: WHAT’S NEW IN ASP.NET MVC 2 471

Security 471

Productivity 473

Performance: Asynchronous Controller Actions 486

Miscellaneous 486

Summary 487

INDEX 489

Code for Professional ASP.NET MVC 2
This archive file contains the code for the entire book, organized by chapter. It includes a README file.
Download
ChapterPageDetailsDatePrint Run
XXXVIIIError in Text
Currently reads: "...conventions and the digs a little..."

Should read: "...conventions and then digs a little..."
01/30/2011

5Error in Text
In the first paragraph after figure 1-7, line 6, the text ".. create reusable and shared the UI ..." should be ".. create reusable and shared UI ..."
01/07/2011

30Error in Code
Code Snippet 1-3 in the DinnerRepository class, the Delete(Dinner dinner) method:
foreach(var rsvp in dinner.RSVPs){
entities.RSVPs.DeleteObject(dinner.RSVPs)
}
This does not work, and throws a compliation error. The error is also repeated in the Code Snippet1-3.txt download. The correct code should be:
foreach(RSVP rsvp in dinner.RSVPs.ToList()){
entities.RSVPs.DeleteObject(rsvp)
}
01/07/2011

35Error in Code
In Code Snippet 1-11, line 9:
"[StringLength(265, ErrorMessage =" is wrong, should be "[StringLength(256, ErrorMessage ="
01/07/2011

61Error in Text
On page 61 in Figure 1-84 the labels for the Title, EventDate and ContactPhone fields inconsistent with the code. These titles can be changed using the DisplayName attribute on these properties in the Dinner class as explained in Chapter 13, page 482, but this step is not shown in Chapter 1. This same problem is evident in other figures later in this chapter.
01/07/2011

74Error in Text
On page 74, the labels for the form displayed in Figure 1-94 are inconsistent with the code in Code Snipped 1-36. The form will look as pictured in Figure 1-94 after completing the Using Partial View Templates section, starting on page 93.
01/07/2011

88Error in Text
On page 88, third paragraph of the Using a ViewModel Pattern section, first sentence, the text states "we can create a DinnerFormViewModel class" but doesn't state where to create this class. This class can be created in the Models folder.
01/07/2011

88Error in Code
On page 88, Code Snippet 1-50, the following should be inserted at the top of the code listing: using System.Web.Mvc;
01/07/2011

89Error in Code
On page 89, code following the first sentence after Code Snippet 1-51, should be changed to read as follows:
Inherits=?System.Web.Mvc.ViewPage<NerdDinner.Models.DinnerFormViewModel>
01/07/2011

90Error in Code
On page 90, Code Snippet 1-53, eighth line should be changed from "if (TryUpdateModel(dinner))" to "if (TryUpdateModel(dinner,"Dinner"))"
01/07/2011

97Error in Text
"consider the following code snippet from the e le in our project"
should be
"consider the following code snippet from the Master Page file in our project"
2-16-11

215Error in Text
Under the heading "StopRoutingHandler and IgnoreRoute"

The last sentence has the word "StopRoutingHandler" repeated. (i.e StopRoutingHandlerStopRoutingHandler)

This should just be StopRoutingHandler
9/30/2010