The ASP.NET MVC 4 Framework is the latest evolution of Microsoft’s ASP.NET web platform. It provides a high-productivity programming model that promotes cleaner code architecture, test-driven development, and powerful extensibility, combined with all the benefits of ASP.NET. ASP.NET MVC 4 contains a number of significant advances over previous versions. New mobile and desktop templates (employing adaptive rendering) are included together with support for jQuery Mobile for the first time. New display modes allow your application to select views based on the browser that's making the request while Code Generation Recipes for Visual Studio help you auto-generate project-specific code for a wide variety of situtations including NuGet support. In this fourth edition, the core model-view-controller (MVC) architectural concepts are not simply explained or discussed in isolation, but are demonstrated in action. You’ll work through an extended tutorial to create a working e-commerce web application that combines ASP.NET MVC with the latest C# language features and unit-testing best practices. By gaining this invaluable, practical experience, you’ll discover MVC’s strengths and weaknesses for yourself—and put your best-learned theory into practice. The book's authors, Steve Sanderson and Adam Freeman, have both watched the growth of ASP.NET MVC since its first release. Steve is a well-known blogger on the MVC Framework and a member of the Microsoft Web Platform and Tools team. Adam started designing and building web applications 15 years ago and has been responsible for some of the world's largest and most ambitious projects. You can be sure you are in safe hands. What you’ll learn Gain a solid architectural understanding of ASP.NET MVC 4, including basic MVC Explore the entire ASP.NET MVC Framework Learn what's new in version 4 and how how best to apply these new features See how MVC and test-driven development work in action Capitalize on your existing knowledge quickly and easily through comparison of features in classic ASP.NET to those in ASP.NET MVC Who this book is for This book is for web developers with a basic knowledge of ASP.NET and C# who want (or need) to start using the new ASP.NET MVC 4 Framework. Table of Contents1. What's The Big Idea 2. Your first MVC Application 3. The MVC Pattern 4. Essential Language Features 5. Working with Razor 6. Essential Tools for MVC 7. Sports Store: A Real Application 8. Sports Store: Navigation 9. Sports Store: Cart 10. Sports Store: Admin 11. Sports Store: Security and Finishing Touches 12. Overview of MVC Projects 13. URL Routing 14. Advanced Routing Features 15. Controllers and Actions 16. Filters 17. Controller Extensbility 18. Views 19. Helper Methods 20. Templated Helper Methods 21. URL and Ajax Helper Methods 22. Model Binding 23. Model Validation 24. Bundles and Display Modes 25. Web API 26. Deployment
Author(s): Adam Freeman
Edition: 4
Publisher: Apress
Year: 2013
Language: English
Pages: 738
Tags: Библиотека;Компьютерная литература;ASP.NET / MVC / Web Services;ASP.NET MVC;
Contents at a Glance......Page 3
Contents......Page 722
About the Author......Page 736
About the Technical Reviewer......Page 737
Acknowledgments......Page 738
A Brief History of Web Development......Page 6
Traditional ASP.NET Web Forms......Page 7
What Is Wrong with ASP.NET Web Forms?......Page 8
Web Standards and REST......Page 9
Ruby on Rails......Page 10
Node.js......Page 11
Extensibility......Page 12
Powerful Routing System......Page 13
ASP.NET MVC Is Open Source......Page 14
Migrating from Web Forms to MVC......Page 15
Summary......Page 16
Preparing the Workstation......Page 17
Creating a New ASP.NET MVC Project......Page 18
Adding the First Controller......Page 21
Understanding Routes......Page 23
Creating and Rendering a View......Page 24
Adding Dynamic Output......Page 27
Setting the Scene......Page 29
Designing a Data Model......Page 30
Linking Action Methods......Page 31
Building the Form......Page 34
Handling Forms......Page 37
Adding Validation......Page 40
Completing the Example......Page 46
Summary......Page 47
Understanding the MVC Pattern......Page 48
Understanding the Domain Model......Page 49
Comparing MVC to Other Patterns......Page 50
Modeling an Example Domain......Page 54
Aggregates and Simplification......Page 55
Defining Repositories......Page 57
Building Loosely Coupled Components......Page 58
Using Dependency Injection......Page 59
An MVC-Specific DI Example......Page 60
Using a Dependency Injection Container......Page 61
Understanding Unit Testing......Page 63
Understanding Integration Testing......Page 71
Summary......Page 72
Creating the Example Project......Page 73
Using Automatically Implemented Properties......Page 74
Using Object and Collection Initializers......Page 78
Using Extension Methods......Page 80
Applying Extension Methods to an Interface......Page 82
Creating Filtering Extension Methods......Page 84
Using Lambda Expressions......Page 86
Using Automatic Type Inference......Page 89
Using Anonymous Types......Page 90
Performing Language Integrated Queries......Page 91
Understanding Deferred LINQ Queries......Page 95
Using Async Methods......Page 97
Applying the async and await Keywords......Page 99
Summary......Page 100
Defining the Model......Page 101
Defining the Controller......Page 102
Creating the View......Page 103
Working with the Model Object......Page 104
Working with Layouts......Page 106
Creating the Layout......Page 107
Applying a Layout......Page 108
Using a View Start File......Page 109
Demonstrating Shared Layouts......Page 110
Using Razor Expressions......Page 114
Inserting Data Values......Page 115
Setting Attribute Values......Page 117
Using Conditional Statements......Page 118
Enumerating Arrays and Collections......Page 121
Dealing with Namespaces......Page 123
Summary......Page 124
Essential Tools for MVC......Page 125
Creating the Model Classes......Page 126
Adding the Controller......Page 127
Using Ninject......Page 128
Understanding the Problem......Page 129
Adding Ninject to the Visual Studio Project......Page 130
Getting Started with Ninject......Page 131
Setting up MVC Dependency Injection......Page 133
Specifying Property and Constructor Parameter Values......Page 138
Using Conditional Binding......Page 140
Unit Testing with Visual Studio......Page 141
Creating the Unit Test Project......Page 142
Creating the Unit Tests......Page 143
Running the Unit Tests (and Failing)......Page 147
Testing and Fixing the Code......Page 148
Understanding the Problem......Page 150
Adding Moq to the Visual Studio Project......Page 152
Adding a Mock Object to a Unit Test......Page 153
Creating a More Complex Mock Object......Page 156
Summary......Page 159
SportsStore—A Real Application......Page 160
Creating the Visual Studio Solution and Projects......Page 161
Adding References......Page 163
Setting Up the DI Container......Page 164
Starting the Application......Page 166
Starting the Domain Model......Page 167
Making a Mock Repository......Page 168
Adding a Controller......Page 169
Adding the View......Page 171
Setting the Default Route......Page 172
Running the Application......Page 173
Creating the Database......Page 174
Defining the Database Schema......Page 176
Creating the Entity Framework Context......Page 178
Creating the Product Repository......Page 180
Adding Pagination......Page 182
Displaying Page Links......Page 184
Improving the URLs......Page 192
Styling the Content......Page 193
Defining Common Content in the Layout......Page 194
Adding CSS Styles......Page 195
Creating a Partial View......Page 196
Summary......Page 198
Filtering the Product List......Page 199
Refining the URL Scheme......Page 203
Building a Category Navigation Menu......Page 205
Correcting the Page Count......Page 214
Defining the Cart Entity......Page 217
Adding the Add to Cart Buttons......Page 221
Implementing the Cart Controller......Page 223
Displaying the Contents of the Cart......Page 224
Summary......Page 228
Creating a Custom Model Binder......Page 229
Removing Items from the Cart......Page 234
Adding the Cart Summary......Page 235
Extending the Domain Model......Page 238
Adding the Checkout Process......Page 239
Implementing the Order Processor......Page 242
Registering the Implementation......Page 245
Completing the Cart Controller......Page 246
Displaying Validation Errors......Page 250
Displaying a Summary Page......Page 251
Summary......Page 252
Creating a CRUD Controller......Page 253
Creating a New Layout......Page 256
Implementing the List View......Page 258
Editing Products......Page 261
Creating New Products......Page 275
Deleting Products......Page 277
Summary......Page 279
Securing the Administration Controller......Page 280
Applying Authorization with Filters......Page 281
Creating the Authentication Provider......Page 283
Creating the Account Controller......Page 285
Creating the View......Page 286
Image Uploads......Page 289
Extending the Database......Page 290
Enhancing the Domain Model......Page 291
Creating the Upload User Interface Elements......Page 292
Saving Images to the Database......Page 293
Implementing the GetImage Action Method......Page 294
Displaying Product Images......Page 296
Summary......Page 297
Working with Visual Studio MVC Projects......Page 299
Understanding MVC Conventions......Page 304
Creating the Project......Page 306
Launching the Visual Studio Debugger......Page 308
Causing the Visual Studio Debugger to Break......Page 310
Using Edit and Continue......Page 315
Summary......Page 318
Creating the Example Project......Page 319
Introducing URL Patterns......Page 322
Creating and Registering a Simple Route......Page 323
Using the Simple Route......Page 326
Defining Default Values......Page 329
Using Static URL Segments......Page 332
Defining Custom Segment Variables......Page 336
Using Custom Variables as Action Method Parameters......Page 339
Defining Optional URL Segments......Page 340
Defining Variable-Length Routes......Page 342
Prioritizing Controllers by Namespaces......Page 344
Constraining a Route to a Set of Specific Values......Page 348
Constraining a Route Using HTTP Methods......Page 349
Defining a Custom Constraint......Page 350
Routing Requests for Disk Files......Page 352
Configuring the Application Server......Page 354
Defining Routes for Disk Files......Page 355
Bypassing the Routing System......Page 356
Summary......Page 357
Preparing the Example Project......Page 358
Using the Routing System to Generate an Outgoing URL......Page 359
Targeting Other Controllers......Page 363
Passing Extra Values......Page 364
Generating Fully Qualified URLs in Links......Page 366
Generating URLs (and Not Links)......Page 367
Generating Outgoing URLs in Action Methods......Page 368
Generating a URL from a Specific Route......Page 369
Creating a Custom RouteBase Implementation......Page 370
Creating a Custom Route Handler......Page 374
Creating an Area......Page 376
Populating an Area......Page 378
Resolving the Ambiguous Controller Issue......Page 380
Generating Links to Actions in Areas......Page 381
Make Your URLs Clean and Human-Friendly......Page 382
GET and POST: Pick the Right One......Page 383
Summary......Page 384
Creating a Controller with IController......Page 385
Creating a Controller by Deriving from the Controller Class......Page 387
Receiving Input......Page 388
Getting Data from Context Objects......Page 389
Using Action Method Parameters......Page 390
Producing Output......Page 392
Understanding Action Results......Page 393
Returning HTML by Rendering a View......Page 397
Passing Data from an Action Method to a View......Page 401
Performing Redirections......Page 405
Returning Errors and HTTP Codes......Page 409
Summary......Page 411
Using Filters......Page 412
Applying Filters to Controllers and Action Methods......Page 414
Creating the Example Project......Page 415
Using Authorization Filters......Page 416
Applying the Custom Authorization Filter......Page 418
Using the Built-in Authorization Filter......Page 419
Creating an Exception Filter......Page 420
Applying the Exception Filter......Page 422
Using a View to Respond to an Exception......Page 424
Using the Built-In Exception Filter......Page 427
Using Action Filters......Page 429
Implementing the OnActionExecuting Method......Page 430
Implementing the OnActionExecuted Method......Page 431
Using Result Filters......Page 433
Using the Built-In Action and Result Filter Class......Page 434
Filtering Without Attributes......Page 436
Using Global Filters......Page 438
Ordering Filter Execution......Page 439
Using the RequireHttps Filter......Page 442
Using the OutputCache Filter......Page 443
Summary......Page 446
Creating the Example Project......Page 447
Creating a Custom Controller Factory......Page 449
Dealing with the Fallback Controller......Page 451
Implementing the Other Interface Methods......Page 452
Registering a Custom Controller Factory......Page 453
Prioritizing Namespaces......Page 454
Customizing DefaultControllerFactory Controller Instantiation......Page 455
Creating a Custom Action Invoker......Page 458
Using the Built-In Action Invoker......Page 460
Using a Custom Action Name......Page 461
Using Action Method Selection......Page 463
Using Sessionless Controllers......Page 468
Using Asynchronous Controllers......Page 470
Summary......Page 475
Creating a Custom View Engine......Page 476
Creating the Example Project......Page 478
Creating a Custom IView......Page 479
Creating an IViewEngine Implementation......Page 480
Testing the View Engine......Page 481
Working with the Razor Engine......Page 483
Understanding Razor View Rendering......Page 484
Configuring the View Search Locations......Page 486
Adding Dynamic Content to a Razor View......Page 488
Using Sections......Page 489
Using Partial Views......Page 495
Using Child Actions......Page 498
Summary......Page 501
Creating the Example Project......Page 502
Creating an Inline Helper Method......Page 504
Creating an External Helper Method......Page 506
Managing String Encoding in a Helper Method......Page 509
Creating Form Elements......Page 514
Specifying the Route Used by a Form......Page 521
Using Input Helpers......Page 523
Creating Select Elements......Page 528
Summary......Page 530
Reviewing the Example Project......Page 531
Using Templated Helper Methods......Page 533
Generating Label and Display Elements......Page 537
Using Whole-Model Template Helpers......Page 540
Using Model Metadata......Page 542
Using Metadata to Control Editing and Visibility......Page 543
Using Metadata for Labels......Page 545
Using Metadata for Data Values......Page 547
Using Metadata to Select a Display Template......Page 549
Applying Metadata to a Buddy Class......Page 551
Working with Complex Type Properties......Page 552
Creating a Custom Editor Template......Page 554
Creating a Generic Template......Page 555
Replacing the Built-in Templates......Page 556
Summary......Page 557
Reviewing and Preparing the Example Project......Page 558
Creating Basic Links and URLs......Page 560
Creating the Synchronous Form View......Page 562
Preparing the Project for Unobtrusive Ajax......Page 564
Preparing the Controller......Page 566
Creating the Ajax Form......Page 567
Understanding How Unobtrusive Ajax Works......Page 569
Ensuring Graceful Degradation......Page 570
Providing the User with Feedback While Making an Ajax Request......Page 571
Prompting the User Before Making a Request......Page 573
Creating Ajax Links......Page 574
Working with Ajax Callbacks......Page 576
Adding JSON Support to the Controller......Page 579
Processing JSON in the Browser......Page 580
Preparing Data for Encoding......Page 582
Detecting Ajax Requests in the Action Method......Page 584
Summary......Page 587
Preparing the Example Project......Page 588
Understanding Model Binding......Page 590
Binding to Simple Types......Page 592
Binding to Complex Types......Page 596
Binding to Arrays and Collections......Page 603
Manually Invoking Model Binding......Page 608
Customizing the Model Binding System......Page 610
Creating a Custom Value Provider......Page 611
Creating a Custom Model Binder......Page 614
Registering the Custom Model Binder......Page 616
Summary......Page 617
Creating the Example Project......Page 618
Explicitly Validating a Model......Page 620
Displaying Validation Errors to the User......Page 622
Displaying Validation Messages......Page 623
Displaying Property-Level Validation Messages......Page 627
Performing Validation in the Model Binder......Page 628
Specifying Validation Rules Using Metadata......Page 630
Defining Self-Validating Models......Page 636
Enabling and Disabling Client-Side Validation......Page 638
Using Client-Side Validation......Page 639
Understanding How Client-Side Validation Works......Page 641
Performing Remote Validation......Page 642
Summary......Page 645
Understanding the Default Script Libraries......Page 646
Preparing the Example Application......Page 648
Profiling Script and Style Sheet Loading......Page 651
Using Script and Style Bundles......Page 653
Applying Bundles......Page 656
Using the Scripts Section......Page 658
Profiling the Changes......Page 659
Taking Stock of the Application......Page 662
Using Mobile Specific Layouts and Views......Page 663
Creating Custom Display Modes......Page 665
Summary......Page 667
Understanding the Web API......Page 668
Creating the Model and Repository......Page 669
Creating the Home Controller......Page 671
Creating the View and the CSS......Page 672
Creating the API Controller......Page 674
Testing the API Controller......Page 676
Understanding API Controller Action Selection......Page 678
Mapping HTTP Methods to Action Methods......Page 679
Writing the JavaScript Code to Use the Web API......Page 680
Creating the Basic Functionality......Page 681
Adding Support for Editing New Reservations......Page 683
Adding Support for Deleting Reservations......Page 685
Adding Support for Creating Reservations......Page 686
Summary......Page 687
Deployment......Page 688
Detecting View Errors......Page 689
Disabling Debug Mode......Page 691
Removing Unused Connection Strings......Page 692
Preparing Windows Azure......Page 693
Creating the Web Site and Database......Page 694
Preparing the Database for Remote Administration......Page 696
Creating the Schema......Page 697
Deploying the Application......Page 699
Summary......Page 705
A......Page 706
B......Page 707
C......Page 708
D......Page 709
F......Page 710
M......Page 711
R......Page 714
S......Page 716
U......Page 717
W, X, Y, Z......Page 718