Thông tin tài liệu
MANNING
Jeffrey Palermo
Jimmy Bogard
Eric Hexter
Matthew Hinze
Jeremy Skinner
FOREWORD BY
Phil Haack
Third edition of
ASP.NET MVC in Action
IN ACTION
Praise for Earlier Editions of
ASP.NET MVC in Action
An authoritative source on ASP.NET MVC 2. Pick up this book!
—Alessandro Gallo, Microsoft MVP
ASP.NET MVC 2 in Action is a good read and an invaluable reference.
—Derek Jackson, Software Architect, Harvard-Westlake
Learn
MVC 2 from the people who helped shape it. Get ready for even more MVC
action in this excellent sequel.
—Alex Thissen, Killer-Apps
Hands-down the best
MVC resource available! Written by the industry’s best and it
shows…so good you may need to buy two copies.
—Andrew Siemer, Software Architect, Lamps Plus
Fully explains fundamental
MVC concepts and best development practices.
—Tetsuo Torigai, Developer, Torigai Consulting
This book doesn’t just explain how to use Microsoft’s
MVC—it teaches practices that
help developers create more maintainable projects.
—Anne Epstein, Senior Consultant, Headspring
ASP.NET in Action is a must-read for anyone who is serious about developing with
the ASP.NET MVC framework.
—Steve Michelotti, Microsoft MVP, geekswithblogs.net
At merely 300 pages, ASP.NET MVC in Action is a true masterpiece…. The authors
are all considered rock stars in the ASP.NET community and they have opened the
doors to their concert with ASP.NET MVC in Action.
—Mohammad Azam, Microsoft MVP
Download from Wow! eBook <www.wowebook.com>
Praise for Earlier Editions of
ASP.NET MVC in Action
This book does a good job of not only showing you what to do, it also provides
cautionary words to avoid poor practices that may lead to maintenance issues on
non-trivial applications.
—Venkat Subramanian, NoFluffJustStuff Blogs
I really enjoyed
ASP.NET MVC in Action and highly recommend it for a fresh look at
the ASP.NET MVC framework.
—David Hayden, Microsoft MVP
ASP.NET MVC in Action will guide you from your first project through advanced
topics such as AJAX and deploying on suboptimal hosting environments. The writing
style is clear and concise. Diagrams and code examples are abundant. I recommend
it for anyone looking for a great resource for learning about or becoming a better user
of the
ASP.NET MVC framework.
—Nathan Stott, Partner and Software Engineer, Whiteboard-IT
I’m very happy with this book. I would definitely recommend it to anyone interested
in ASP.NET MVC. Getting beyond the text that comes with the CodeCampServer is
just icing on the cake.
—Chris Stewart, CompiledMonkey.com
The authors not only did an excellent job of putting together a great practical guide
to
ASP.NET MVC, they also successfully embedded some subversive ALT.NET concepts
that will make us all better developers. And at the end of the day, that is a damn fine
accomplishment!
—Bobby Johnson, AppExtremes
As my first introduction to MVC, I found this book very readable and interesting.
—Roger Wright, Engineering Manager, Aha Macav Power Service
Download from Wow! eBook <www.wowebook.com>
ASP.NET MVC 4
in Action
A revised edition of ASP.NET MVC 2 in Action
JEFFREY PALERMO, JIMMY BOGARD
ERIC HEXTER, MATTHEW HINZE
AND JEREMY SKINNER
MANNING
Shelter Island
Download from Wow! eBook <www.wowebook.com>
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.
20 Baldwin Road
PO Box 261
Shelter Island, NY 11964
Email: orders@manning.com
©2012 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 percent recycled and processed without the use of elemental
chlorine.
Manning Publications Co. Development editor: Cynthia Kane
20 Baldwin Road Technical proofreader: Javier Lozano
PO Box 261 Copyeditor: Andy Carroll
Shelter Island, NY 11964 Proofreader: Maureen Spencer
Cover designer: Marija Tudor
Typesetter: Gordan Salinovic
ISBN 9781617290411
Printed in the United States of America
1 2 3 4 5 6 7 8 9 10 – MAL – 17 16 15 14 13 12
Download from Wow! eBook <www.wowebook.com>
v
brief contents
PART 1 HIGH-SPEED FUNDAMENTALS 1
1 ■ Introduction to ASP.NET MVC 3
2 ■ Hello MVC world 12
3 ■ View fundamentals 38
4 ■ Action-packed controllers 59
PART 2 WORKING WITH ASP.NET MVC 79
5 ■ View models 81
6 ■ Validation 92
7 ■ Ajax in ASP.NET MVC 104
8 ■ Security 135
9 ■ Controlling URLs with routing 153
10 ■ Model binders and value providers 185
11 ■ Mapping with AutoMapper 197
12 ■ Lightweight controllers 207
13 ■ Organization with areas 220
14 ■ Third-party components 232
15 ■ Data access with NHibernate 244
Download from Wow! eBook <www.wowebook.com>
BRIEF CONTENTS
vi
PART 3 MASTERING ASP.NET MVC 265
16 ■ Extending the controller 267
17 ■ Advanced view techniques 276
18 ■ Dependency injection and extensibility 294
19 ■ Portable areas 311
20 ■ Full system testing 321
21 ■ Hosting ASP.NET MVC applications 339
22 ■ Deployment techniques 365
23 ■ Upgrading to ASP.NET MVC 4 374
24 ■ ASP.NET Web API 385
Download from Wow! eBook <www.wowebook.com>
vii
contents
foreword xv
foreword to the second edition xvi
foreword to the first edition xvii
preface xix
acknowledgments xxiii
about this book xxvi
about the authors xxix
about the cover illustration xxxii
PART 1 HIGH-SPEED FUNDAMENTALS 1
1
Introduction to ASP.NET MVC 3
1.1 Setting the stage 4
The .NET platform 4
■
ASP.NET Web Forms 5
1.2 What is ASP.NET MVC? 5
The MVC pattern 7
■
Benefits of ASP.NET MVC 8
1.3 What’s new in ASP.NET MVC 3/4? 8
The Razor view engine 9
■
Package management with NuGet 9
Improved extensibility 10
■
Global action filters 10
■
Dynamic
language features 10
■
Partial page output caching 10
Ajax improvements 10
■
Validation improvements 10
1.4 Summary 11
Download from Wow! eBook <www.wowebook.com>
CONTENTS
viii
2
Hello MVC world 12
2.1 Setting up your development environment 13
Installing MVC using the Web Platform Installer 13
2.2 Creating your first MVC application 15
Creating a new project 15
■
A tour of the default project template 17
Controllers, actions, and displaying dynamic content 19
2.3 The Guestbook sample application 23
Creating the database 23
■
Adding the model 24
■
Accepting
guestbook entries 28
■
Displaying guestbook entries 33
Customizing the look and feel with layouts 35
2.4 Summary 37
3
View fundamentals 38
3.1 Introducing views 38
Selecting a view to render 39
■
Overriding the view name 40
3.2 Passing data to views 40
Examining the ViewDataDictionary 40
■
The ViewBag 42
Strongly typed views with a view model 43
■
Displaying view model
data in a view 44
3.3 Using strongly typed templates 48
EditorFor and DisplayFor templates 49
■
Built-in templates 51
Selecting templates 52
■
Customizing templates 54
3.4 Summary 58
4
Action-packed controllers 59
4.1 Exploring controllers and actions 60
IController and the controller base classes 60
■
What makes an
action method 62
4.2 What should be in an action method? 63
Manually mapping view models 64
■
Input validation 66
4.3 Introduction to unit testing 69
Using the provided test project 69
■
Testing the
GuestbookController 71
4.4 Summary 77
Download from Wow! eBook <www.wowebook.com>
CONTENTS
ix
PART 2 WORKING WITH ASP.NET MVC 79
5
View models 81
5.1 What is a view model? 82
The online store example 82
■
Building the view model 84
Delivering the presentation model 84
■
ViewData.Model 85
5.2 Representing user input 86
Designing the model 86
■
Presenting the input model in a
view 87
■
Working with the submitted input 88
5.3 More complex models for both display and input 89
Designing a combined display and input model 90
■
Working with
the input model 90
5.4 Summary 91
6
Validation 92
6.1 Server-side validation 93
Validation with Data Annotations 93
■
Extending the
ModelMetadataProvider 96
6.2 Client-side validation 98
Getting started with client-side validation 99
■
Using
RemoteAttribute 100
■
Creating custom client-side validators 101
6.3 Summary 103
7
Ajax in ASP.NET MVC 104
7.1 Ajax with jQuery 105
jQuery primer 106
■
Using jQuery to make Ajax requests 107
Progressive enhancement 109
■
Using Ajax to submit form data 111
7.2 ASP.NET MVC Ajax helpers 114
Ajax.ActionLink 116
■
Ajax.BeginForm 117
■
Ajax options 118
Differences from earlier versions of ASP.NET MVC 119
7.3 Ajax with JSON and client templates 120
Ajax with JSON 120
■
Client-side templates 124
■
Finishing
touches 126
7.4 Creating an autocomplete text box 129
Building the CitiesController 129
7.5 Summary 134
Download from Wow! eBook <www.wowebook.com>
[...]... version, ASP.NET MVC 2 For the second edition, we brought on two new members to the author team: Eric Hexter and Matthew Hinze The five of us started working on ASP.NET MVC 2 in Action in late 2009 with framework knowledge we cultivated in the field and experience as authors that we’d gained writing the first book With ASP.NET MVC 4 in Action, Ben moved into iOS development, and Jeremy Skinner joined the... Summary 320 Full system testing 321 20.1 Testing the UI layer 322 Installing the testing software 322 Walking through the test manually 323 Automating the test 325 Running the test 327 ■ ■ 20.2 20.3 20.4 20.5 21 ■ Building maintainable navigation Interacting with forms 331 Asserting results 334 Summary 338 327 Hosting ASP.NET MVC applications 339 21.1 21.2 21.3 21.4 21.5 Hosting environments 340 XCOPY... actions 281 Building query-string parameter lists 282 Exploring the Spark view engine 285 Installing and configuring Spark example 287 18 ■ 286 ■ Simple Spark view Summary 292 Dependency injection and extensibility 294 18.1 Introducing dependency injection 295 What is DI 296 Using constructor injection 297 interfaces 298 Using a DI container 299 ■ ■ ■ 18.2 Using DI with ASP.NET MVC Custom controller factories... 9.4 9.5 ■ Using the routing system to generate URLs Routing with ASP.NET Web Forms 169 Adding routes for Web Forms pages 169 Web Forms pages 172 9.6 Debugging routes Testing route behavior 10 Summary ■ Using Route Debugger 174 178 Testing inbound routes 178 9.8 Generating URLs from 173 Installing Route Debugger 173 Using route constraints 176 9.7 ■ ■ Testing outbound routes 183 Model binders and value... source code in listings or in text is in a fixed-width font like this to separate it from ordinary text Code annotations accompany many of the listings, highlighting important concepts In some cases, numbered bullets link to explanations that follow the listing The source code for the examples in this book is available online from the publisher’s website at http://www.manning.com/ASP.NETMVC4inAction Author... book: ASP.NET MVC in Action The book you hold in your hands is the product of hundreds of hours of real world experience, experimentation, and documentation of how to best use the newest version of the Microsoft ASP.NET MVC framework In ASP.NET MVC 2 in Action you will learn from expert users of the ASP.NET MVC framework on all subjects: Routes, Controllers, Controller Factories, View Engines, Input... later, it’s a fundamental shift in thinking because they grew up with Web Forms being “normal” web development This book starts by providing an introduction to ASP.NET MVC, which should be helpful if you’ve never used ASP.NET MVC before, or if you have experience with a previous version and are interested in seeing what’s new in version 3 Following this, we’ll dive deeper into the core concepts that are... means to achieving that goal After Preview 2 of ASP.NET MVC was released, we received a lot of feedback from developers that writing unit tests with ASP.NET MVC was difficult Jeffrey Palermo, the lead author of ASP.NET MVC in Action, was among the most vocal in providing feedback during this time We took this feedback and implemented a major API change by introducing the concept of action results,... the key concepts in depth Before you begin chapter 1, you will want to install ASP.NET MVC 4 and Visual Studio 2010 or 2011 In chapter 1, we walk through a beginner ramp-up, covering the basics of the MVC pattern and ASP.NET MVC implementation Chapter 2 takes you through implementing a simple Hello World example Next, chapter 3 covers the fundamentals of MVC views, including creating strongly typed... time they shipped ASP.NET MVC, the release was ASP.NET MVC Tools Update The ASP.NET MVC runtime did not change at all In fact, it was the same exact runtime installer But wow did the tooling change! ASP.NET MVC included scaffolding based on the Entity Framework Code First model This provided all the code needed for a simple CRUD interface over a set of entities Also included in that release was NuGet . 1 1 Introduction to ASP. NET MVC 3 1.1 Setting the stage 4 The .NET platform 4 ■ ASP. NET Web Forms 5 1.2 What is ASP. NET MVC? 5 The MVC pattern 7 ■ Benefits of ASP. NET MVC 8 1.3 What’s new in ASP. NET. shipped ASP. NET MVC, the release was ASP. NET MVC Tools Update. The ASP. NET MVC runtime did not change at all. In fact, it was the same exact runtime installer. But wow did the tooling change! ASP. NET. Microsoft ASP. NET MVC framework. In ASP. NET MVC 2 in Action you will learn from expert users of the ASP. NET MVC framework on all subjects: Routes, Controllers, Controller Factories, View Engines, Input
Ngày đăng: 28/03/2014, 23:20
Xem thêm: ASP.Net MVC in Action pot, ASP.Net MVC in Action pot, 4 NHibernate configuration–infrastructure of the application, 3 Action, authorization, and result filters