Programming microsoft ASP NET MVC covers ASP NET MVC2 and microsoft visual strudio 2010

590 466 2
Programming microsoft ASP NET MVC covers ASP NET MVC2 and microsoft visual strudio 2010

Đang tải... (xem toàn văn)

Tài liệu hạn chế xem trước, để xem đầy đủ mời bạn chọn Tải xuống

Thông tin tài liệu

We started discussing this book around the release of ASP.NET MVC 1.0, in the late spring of 2009. It was not exactly an ideal time for making plans, even though business had to go on in spite of the world financial crisis and severe downturn in the economy. Now that the book is finished, we seem to live in slightly better times, and we all sincerely hope that the worst of it is behind us. However, as I look at this book project now that it’s finished, I realize I’m deeply missing one special person—Lynn Finnel. Lynn was laid off in the middle of the project as a result of one of the many restructurings that a lot of companies went through in the past year. Lynn and I have done so many books together, and we always shared a mutual high level of satisfaction with the outcome. Not sending chapters and reviews to Lynn any more was a big change in this part of my professional life. And, who knows, one day I might have Lynn, at the height of her new career as a physical therapist, just take care of my poor back, stressed by its unnatural posture and too many hours of tennis. Changing the project editor in the middle of a book project can sometimes be a tough experience, but as arranged by Carol Vu the transition was seamless. Despite the difficulties in replacing an editor of Lynn’s caliber, I really didn’t notice any difference. In the past few months, Ben Ryan got a bunch of e-mails from me asking, with different tones, always the same question: “Are you still there?” Yes, fortunately, he’s still there with prompt and insightful suggestions. Cheap and valuable—only two cents each! But I’ve never valued any advice more than Ben’s two-cent propositions. And, fortunately, Kenn Scribner is still part of the team and a rock-solid pillar. The degree to which Kenn can be helpful is just beyond human imagination. Now Kenn, let me challenge you: I wrote this same sentence about you in another book. Which one? Hey, you have only a few days to answer as another project is in sight! :)

PUBLISHED BY Microsoft Press A Division of Microsoft Corporation One Microsoft Way Redmond, Washington 98052-6399 Copyright © 2010 by Dino Esposito All rights reserved. No part of the contents of this book may be reproduced or transmitted in any form or by any means without the written permission of the publisher. Library of Congress Control Number: 2010925900 Printed and bound in the United States of America. 1 2 3 4 5 6 7 8 9 WCT 5 4 3 2 1 0 Distributed in Canada by H.B. Fenn and Company Ltd. A CIP catalogue record for this book is available from the British Library. Microsoft Press books are available through booksellers and distributors worldwide. For further information about international editions, contact your local Microsoft Corporation office or contact Microsoft Press International directly at fax (425) 936-7329. Visit our Web site at www.microsoft.com/mspress. Send comments to mspinput@microsoft.com. Microsoft, Microsoft Press, ActiveX, Excel, IntelliSense, Internet Explorer, MS, MSDN, SharePoint, Silverlight, SQL Server, Visual Basic, Visual C#, Visual Studio, Win32, Windows, Windows Server, and Windows Vista. Other product and company names mentioned herein may be the trademarks of their respective owners. The example companies, organizations, products, domain names, e-mail addresses, logos, people, places, and events depicted herein are fictitious. No association with any real company, organization, product, domain name, e-mail address, logo, person, place, or event is intended or should be inferred. This book expresses the author’s views and opinions. The information contained in this book is provided without any express, statutory, or implied warranties. Neither the authors, Microsoft Corporation, nor its resellers, or distributors will be held liable for any damages caused or alleged to be caused either directly or indirectly by this book. Acquisitions Editor: Ben Ryan Developmental Editor: Lynn Finnel Project Editors: Lynn Finnel and Carol Vu Editorial Production: Ashley Schneider, S4Carlisle Publishing Services Technical Reviewer: Kenn Scribner; Technical Review services provided by Content Master, a member of CM Group, Ltd Cover: Tom Draper Design Body Part No. X16-88503 To Silvia, Francesco, and Michela, who wait for me and keep me busy. But I’m happy only when I’m busy. —Dino v Contents at a Glance Part I The Programming Paradigm 1 Goals of ASP.NET MVC and Motivation for Its Development 3 2 The Runtime Environment 37 3 The MVC Pattern and Beyond .81 Part II The Core of ASP.NET MVC 4 Inside Controllers 123 5 Inside Views .211 6 Inside Models .277 Part III Programming Features 7 Data Entry in ASP.NET MVC 317 8 The ASP.NET MVC Infrastructure .355 9 AJAX Capabilities .401 10 Testability and Unit Testing 435 11 Customizing ASP.NET MVC 477 vii Table of Contents Acknowledgments xiii Introduction .xv Part I The Programming Paradigm 1 Goals of ASP.NET MVC and Motivation for Its Development 3 The Deep Impact of ASP.NET .5 Productivity Is King . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6 The Web Forms Model .9 The “Page Controller” Pattern 11 The ASP.NET Age of Reason .16 ASP.NET’s Signs of Aging 16 The Turning Point .20 ASP.NET MVC at a Glance .26 ASP.NET MVC Highlights 26 Web Forms vs. ASP.NET MVC .30 Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 35 2 The Runtime Environment 37 The ASP.NET Runtime Machinery 37 ASP.NET and the IIS Web Server 38 Life Cycle of an ASP.NET Request .44 What’s an HTTP Handler, Anyway? 51 What’s an HTTP Module, Anyway? 57 URL Routing .61 The ASP.NET MVC Run-Time Shell .67 The Big Picture .68 Processing an ASP.NET MVC Request 75 Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 80 Microsoft is interested in hearing your feedback so we can continually improve our books and learning resources for you. To participate in a brief online survey, please visit: www.microsoft.com/learning/booksurvey/ What do you think of this book? We want to hear from you! viii Table of Contents 3 The MVC Pattern and Beyond . 81 The Original MVC Pattern .82 MVC Interaction Model .82 The Original Idea .83 Presenting the Actors .84 Limitations of the MVC Pattern .89 The Model2 Pattern 90 MVC and the Web 90 Model2 and ASP.NET MVC .93 Presentation-Oriented Variations of MVC .98 The MVP Pattern .98 Presentation Model Pattern (Also Known as MVVM) .103 The ASP.NET MVC Project Template .107 Peculiarities of an ASP.NET MVC Project 108 ASP.NET MVC Special Folders .113 Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 118 Part II The Core of ASP.NET MVC 4 Inside Controllers 123 The Role of Controllers and the Motivation for Using Them 123 Beyond the Code-Behind Approach .124 Introducing Controllers .128 Mechanics of Controllers in ASP.NET MVC 132 Anatomy of an ASP.NET MVC Controller .135 Inside the Structure of a Controller .135 Behavior of a Controller 144 Attributes of Controllers and Action Methods .156 Writing a Controller .167 Design of a Controller Class .167 Should You Use Your Own Base Class? 176 Special Capabilities 183 Grouping Controllers .184 Asynchronous Controllers 187 Render Actions 195 Controllers and Testability .199 Making Controllers Easy to Test .199 Writing Unit Tests 204 Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 209 Table of Contents ix 5 Inside Views .211 Views and Controllers .212 From Controllers to Views 212 Building the Response for the Browser .213 Anatomy of an ASP.NET MVC View 215 Selecting the View .215 Creating the View .220 The Default View Engine 222 The Web Forms View Engine 226 Writing a View .233 The View’s Template .235 Filling Up the View .241 HTML Helpers .252 Templated HTML Helpers .257 Datagrids and Paged Views .261 Testing a View .273 Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 275 6 Inside Models .277 What’s the Model, Anyway? .278 How Many Types of Models Do You Know? .278 The Models Folder .282 Domain Model and View-Model 286 Business Object Modeling 286 Adding Validation Logic to the Model 291 Data for the View 299 Model Binding .305 The Model Binder in Action .306 Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 313 Part III Programming Features 7 Data Entry in ASP.NET MVC .317 The Select-Edit-Save Pattern .318 Presenting Data .318 Editing Data 320 Saving Data .324 Data Validation .326 Validation on the Server Side 326 x Table of Contents Giving Feedback to the User 334 Data Annotations and Validators 342 Client-Side Validation 351 Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 354 8 The ASP.NET MVC Infrastructure .355 Routing 356 Dealing with Routes 356 Keeping an Eye on SEO 362 Error Handling .366 Foundations of ASP.NET Error Handling 366 Dealing with Missing Content .371 Localization 374 Making Resources Localizable 374 Dealing with Resources in ASP.NET MVC 376 Dependency Injection .382 Dependency Inversion in Action 383 A Brief Tour of Unity .391 Creating a Global Container 395 Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 399 9 AJAX Capabilities .401 AJAX in ASP.NET 401 Partial Rendering 402 Direct Scripting .405 AJAX in ASP.NET MVC .408 The JavaScript API .408 The Controller Façade .413 AJAX Helpers in ASP.NET MVC 420 Partial Rendering in ASP.NET MVC .428 Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 432 10 Testability and Unit Testing 435 Testability and Design .436 Design for Testability .436 Loosen Up Your Design .438 Basics of Unit Testing 443 Working with a Test Harness . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 443 Aspects of Testing .447 . there (and mostly valid) at http://www.dotnetslackers.com/articles/aspnet/AnArchitecturalViewOfTheASPNETMVCFramework .aspx. The taste of ASP. NET MVC was. of ASP. NET MVC. ASP. NET what? I had a look at the bits, and a few weeks later I even wrote one of the very first articles about ASP. NET MVC for the DotNetSlackers

Ngày đăng: 22/08/2013, 14:06

Từ khóa liên quan

Tài liệu cùng người dùng

  • Đang cập nhật ...

Tài liệu liên quan