Manning ASP.NET MVC in Action (2010).pdf

(8251 KB) Pobierz
ASP.NET MVC
in Action
WITH M VC C ONTRIB , NH IBERNATE , AND MORE
JEFFREY PALERMO
BEN SCHEIRMAN
JIMMY BOGARD
MANNING
Greenwich
(74° w. long.)
1015577883.001.png 1015577883.002.png
For online information and ordering of this and other Manning books, please visit
www.manning.com . The publisher offers discounts on this book when ordered in quantity.
For more information, please contact
Special Sales Department
Manning Publications Co.
Sound View Court 3B
fax: (609) 877-8256
Greenwich, CT 06830
email: orders@manning.com
©2010 by Manning Publications Co. All rights reserved.
No part of this publication may be reproduced, stored in a retrieval system, or transmitted, in
any form or by means electronic, mechanical, photocopying, or otherwise, without prior written
permission of the publisher.
Many of the designations used by manufacturers and sellers to distinguish their products are
claimed as trademarks. Where those designations appear in the book, and Manning
Publications was aware of a trademark claim, the designations have been printed in initial caps
or all caps.
Recognizing the importance of preserving what has been written, it is Manning’s policy to have
the books we publish printed on acid-free paper, and we exert our best efforts to that end.
Recognizing also our responsibility to conserve the resources of our planet, Manning books are
printed on paper that is at least 15% recycled and processed without the use of elemental chlorine.
Development Editor:
Tom Cirtin
Copyeditor:
Betsey Henkels
Manning Publications Co.
Proofreader:
Elizabeth Martin
Sound View Court 3B
Typesetter:
Gordan Salinovic
Greenwich, CT 06830
Cover designer:
Leslie Haimes
ISBN 978-1-933988-62-7
Printed in the United States of America
1 2 3 4 5 6 7 8 9 10 – MAL – 14 13 12 11 10 09
1015577883.003.png 1015577883.004.png
brief contents
1
Getting started with the ASP.NET MVC Framework
1
2
The model in depth
24
3
The controller in depth
44
4
The view in depth
65
5
Routing 91
6
Customizing and extending the ASP.NET MVC Framework
119
7
Scaling the architecture for complex sites
152
8
Leveraging existing ASP.NET features
174
9
AJAX in ASP.NET MVC
195
10
Hosting and deployment
216
11
Exploring MonoRail and Ruby on Rails
238
12
Best practices
270
13
Recipes 312
v
 
contents
foreword xiii
preface xv
acknowledgments xviii
about this book xxi
about the authors xxvi
about the cover illustration xxviii
1 Getting started with the ASP.NET MVC Framework
1
1.1
Picking apart the default application
3
Creating the project
4 Your first routes
7 Running with the
starter project
9
1.2
Your first ASP.NET MVC controller from scratch
14
1.3
Our first view
16
1.4
Ensuring the application is maintainable
18
1.5
Testing controller classes
20
1.6 Summary 22
2 The model in depth
24
2.1
Understanding the basics of domain-driven design
25
2.2
Domain model for this book
26
Key entities and value objects
26 Aggregates
27 Persistence
for the domain model
29
vii
viii
CONTENTS
2.3
Presentation model 31
Presentation model responsibilities
31 Projecting from the
domain model
33
2.4
Working with the model
34
Crafting the route
35 Crafting the controller action
35 Test-
driving the feature
36 Finishing the view
39
2.5 Summary 42
3 The controller in depth
44
3.1
The controller action
45
3.2
Simple controllers do not need a view
47
3.3
Testing controllers 50
Testing the RedirectController
50 Making dependencies
explicit
52 Using test doubles, such as stubs and
mocks
53 Elements of a good controller unit test
55
3.4
Simple actions and views
56
3.5
Working with form values
57
3.6
Processing querystring parameters
58
3.7
Binding more complex objects in action parameters
59
3.8
Options for passing ViewData
61
3.9 Filters 62
3.10 Summary 64
4 The view in depth
65
4.1
How ASP.NET MVC views differ from Web Forms
66
4.2
Folder structure and view basics
67
4.3
Overview of view basics 69
Examining the IViewEngine abstraction 70 Understanding
master pages in the ASP.NET MVC Framework
71 Using
ViewData to send objects to a view
73 Partial views can help
decompose a complex screen
76
4.4
Leveraging the view to create dynamic screens
79
Rendering forms with view helpers and data binding
79 Posting
HTML forms back to the server
84 Validation and error
reporting
85 Extending HtmlHelper
88
4.5
Summary 90
Zgłoś jeśli naruszono regulamin