asp.net mvc 4 recipes - link.springer.com3a978-1-4302-4774-6%2f1.pdf · isbn 978-1-4302-4774-6...

24
ASP.NET MVC 4 Recipes A Problem-Solution Approach John Ciliberti Apress

Upload: others

Post on 08-Oct-2019

4 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: ASP.NET MVC 4 Recipes - link.springer.com3A978-1-4302-4774-6%2F1.pdf · ISBN 978-1-4302-4774-6 (eBook) This book is dedicated to all of the developers who stay up to 3AM for weeks

ASP.NET MVC 4 RecipesA Problem-Solution Approach

John Ciliberti

Apress

Page 2: ASP.NET MVC 4 Recipes - link.springer.com3A978-1-4302-4774-6%2F1.pdf · ISBN 978-1-4302-4774-6 (eBook) This book is dedicated to all of the developers who stay up to 3AM for weeks

ASP.NET MVC 4 Recipes

Copyright © 2013 by John Ciliberti

This work is subject to copyright. All rights are reserved by the Publisher, whether the whole or part of the material is concerned, specifically the rights of translation, reprinting, reuse of illustrations, recitation, broadcasting, reproduction on microfilms or in any other physical way, and transmission or information storage and retrieval, electronic adaptation, computer software, or by similar or dissimilar methodology now known or hereafter developed. Exempted from this legal reservation are brief excerpts in connection with reviews or scholarly analysis or material supplied specifically for the purpose of being entered and executed on a computer system, for exclusive use by the purchaser of the work. Duplication of this publication or parts thereof is permitted only under the provisions of the Copyright Law of the Publisher’s location, in its current version, and permission for use must always be obtained from Springer. Permissions for use may be obtained through RightsLink at the Copyright Clearance Center. Violations are liable to prosecution under the respective Copyright Law.

Trademarked names, logos, and images may appear in this book. Rather than use a trademark symbol with every occurrence of a trademarked name, logo, or image we use the names, logos, and images only in an editorial fashion and to the benefit of the trademark owner, with no intention of infringement of the trademark.

The use in this publication of trade names, trademarks, service marks, and similar terms, even if they are not identified as such, is not to be taken as an expression of opinion as to whether or not they are subject to proprietary rights.

While the advice and information in this book are believed to be true and accurate at the date of publication, neither the authors nor the editors nor the publisher can accept any legal responsibility for any errors or omissions that may be made. The publisher makes no warranty, express or implied, with respect to the material contained herein.

President and Publisher: Paul ManningLead Editor: Jonathan HassellDevelopmental Editor: Tom WelshTechnical Reviewer: Damien FoggonEditorial Board: Steve Anglin, Mark Beckner, Ewan Buckingham, Gary Cornell, Louise Corrigan, Morgan Ertel,

Jonathan Gennick, Jonathan Hassell, Robert Hutchinson, Michelle Lowman, James Markham, Matthew Moodie, Jeff Olson, Jeffrey Pepper, Douglas Pundick, Ben Renow-Clarke, Dominic Shakeshaft, Gwenan Spearing, Matt Wade, Tom Welsh

Coordinating Editor: Anamika PanchooCopy Editor: Kimberly Burton-WeismanCompositor: SPi GlobalIndexer: SPi GlobalArtist: SPi GlobalCover Designer: Anna Ishchenko

Distributed to the book trade worldwide by Springer Science+Business Media New York, 233 Spring Street, 6th Floor, New York, NY 10013. Phone 1-800-SPRINGER, fax (201) 348-4505, e-mail [email protected], or visit www.springeronline.com. Apress Media, LLC is a California LLC and the sole member (owner) is Springer Science + Business Media Finance Inc (SSBM Finance Inc). SSBM Finance Inc is a Delaware corporation.

For information on translations, please e-mail [email protected], or visit www.apress.com.

Apress and friends of ED books may be purchased in bulk for academic, corporate, or promotional use. eBook versions and licenses are also available for most titles. For more information, reference our Special Bulk Sales–eBook Licensing web page at www.apress.com/bulk-sales.

Any source code or other supplementary materials referenced by the author in this text is available to readers at www.apress.com. For detailed information about how to locate your book’s source code, go to www.apress.com/source-code/.

ISBN 978-1-4302-4773-9

ISBN 978-1-4302-4774-6 (eBook)

Page 3: ASP.NET MVC 4 Recipes - link.springer.com3A978-1-4302-4774-6%2F1.pdf · ISBN 978-1-4302-4774-6 (eBook) This book is dedicated to all of the developers who stay up to 3AM for weeks

This book is dedicated to all of the developers who stay up to 3AM for weeks at a time turning their ideas into reality and improving the lives of millions of people around the world.

Page 4: ASP.NET MVC 4 Recipes - link.springer.com3A978-1-4302-4774-6%2F1.pdf · ISBN 978-1-4302-4774-6 (eBook) This book is dedicated to all of the developers who stay up to 3AM for weeks

v

Contents at a Glance

About the Author ............................................................................................................. xxiii

About the Technical Reviewer .......................................................................................... xxv

Acknowledgments .......................................................................................................... xxvii

Introduction ..................................................................................................................... xxix

Chapter 1: The Need for Modern Web Applications ...........................................................1

Chapter 2: Understanding ASP.NET MVC ..........................................................................15

Chapter 3: Setting Up Your Environment .........................................................................59

Chapter 4: Visual Studio 2012 Overview .......................................................................105

Chapter 5: Getting the Most from the Built-in Templates ..............................................139

Chapter 6: Architecting Applications with ASP.NET MVC ...............................................185

Chapter 7: Solution Design ............................................................................................215

Chapter 8: Asynchronous Programming with ASP.NET MVC .........................................281

Chapter 9: Test-Driven Development with ASP.NET MVC 4 ............................................321

Chapter 10: Moving From Web Forms to ASP.NET MVC .................................................375

Chapter 11: Creating Modern User Experiences Using jQuery, Knockout.js, and Web API ....475

Chapter 12: Mobile, Social, and Cloud Technologies .....................................................543

Index .................................................................................................................................589

Page 5: ASP.NET MVC 4 Recipes - link.springer.com3A978-1-4302-4774-6%2F1.pdf · ISBN 978-1-4302-4774-6 (eBook) This book is dedicated to all of the developers who stay up to 3AM for weeks

vii

Contents

About the Author ............................................................................................................. xxiii

About the Technical Reviewer .......................................................................................... xxv

Acknowledgments .......................................................................................................... xxvii

Introduction ..................................................................................................................... xxix

Chapter 1: The Need for Modern Web Applications ...........................................................1

1-1. Developing Modern Web Applications on the Microsoft Platform ..........................................2

Is ASP.NET a Dying Technology? ............................................................................................................................ 3

1-2. Learning Through Recipes .....................................................................................................5

1-3. An Outline of This Book ..........................................................................................................5

Chapter 2: Understanding ASP.NET MVC ................................................................................................................ 5

Chapter 3: Setting Up Your Environment ................................................................................................................ 5

Chapter 4: Visual Studio 2012 Overview ................................................................................................................ 5

Chapter 5: Getting the Most from the Built-in Templates ...................................................................................... 5

Chapter 6: Architecting Applications with ASP.NET MVC ........................................................................................ 6

Chapter 7: Solution Design .................................................................................................................................... 6

Chapter 8: Asynchronous Programming with ASP.NET MVC .................................................................................. 6

Chapter 9: Test-Driven Development with ASP.NET MVC 4 .................................................................................... 6

Chapter 10: Moving from Web Forms to ASP.NET MVC .......................................................................................... 6

Chapter 11: Creating Modern User Experiences Using jQuery, Knockout, and Web API ......................................... 7

Chapter 12: Mobile, Social, and Cloud Technologies .............................................................................................. 7

1-4. Finding the Recipes That You Need ........................................................................................7

Recommended Recipes for People New to Web Development .............................................................................. 7

Recommended Recipes for Experienced ASP.NET Web Forms Developers ........................................................... 8

Page 6: ASP.NET MVC 4 Recipes - link.springer.com3A978-1-4302-4774-6%2F1.pdf · ISBN 978-1-4302-4774-6 (eBook) This book is dedicated to all of the developers who stay up to 3AM for weeks

CONTENTS

viii

Recommended Recipes for Architects and Technical Leads ................................................................................. 9

Recommended Recipes for Cloud Developers ....................................................................................................... 9

Recommended Recipes for Developers New to jQuery and Knockout ................................................................ 10

1-5. The Code Samples ...............................................................................................................10

About the Sample Database ................................................................................................................................ 11

Install SQL Server Express with Tools .................................................................................................................. 11

Installing the Sample Database ........................................................................................................................... 12

About the Shared Library ..................................................................................................................................... 13

Chapter 2: Understanding ASP.NET MVC ..........................................................................15

2-1. Understanding the Microsoft Web Development Ecosystem ...............................................15

Problem ............................................................................................................................................................... 15

Solution................................................................................................................................................................ 15

How It Works ........................................................................................................................................................ 15

2-2. Understanding the Differences Between the Versions of the MVC Framework ...................22

Problem ............................................................................................................................................................... 22

Solution................................................................................................................................................................ 23

How It Works ........................................................................................................................................................ 23

2-3. Understanding the MVC Pattern ...........................................................................................25

Problem ............................................................................................................................................................... 25

Solution................................................................................................................................................................ 25

How It Works ........................................................................................................................................................ 26

2-4. Understanding the Differences Between MVC, MVVM, and MVP ................................................28

Problem ............................................................................................................................................................... 28

Solution................................................................................................................................................................ 28

How It Works ........................................................................................................................................................ 28

2-5. Deciding Between MVC and Web Forms ..............................................................................30

Problem ............................................................................................................................................................... 30

Solution................................................................................................................................................................ 30

How It Works ........................................................................................................................................................ 30

Page 7: ASP.NET MVC 4 Recipes - link.springer.com3A978-1-4302-4774-6%2F1.pdf · ISBN 978-1-4302-4774-6 (eBook) This book is dedicated to all of the developers who stay up to 3AM for weeks

CONTENTS

ix

2-6. Understanding the ASP.NET MVC Framework Architecture ..................................................35

Problem ............................................................................................................................................................... 35

Solution................................................................................................................................................................ 35

How It Works ........................................................................................................................................................ 35

2-7. Understanding Models .........................................................................................................38

Problem ............................................................................................................................................................... 38

Solution................................................................................................................................................................ 38

How It Works ........................................................................................................................................................ 39

Creating Composite Models ................................................................................................................................. 40

Using the Entity Framework ................................................................................................................................ 40

2-8. Understanding Controllers and Actions................................................................................40

Problem ............................................................................................................................................................... 40

Solution................................................................................................................................................................ 40

How It Works ........................................................................................................................................................ 41

2-9. Understanding Page Routing ...............................................................................................42

Problem ............................................................................................................................................................... 42

Solution................................................................................................................................................................ 42

How It Works ........................................................................................................................................................ 43

2-10. Understanding View Engines .............................................................................................44

Problem ............................................................................................................................................................... 44

Solution................................................................................................................................................................ 44

How It Works ........................................................................................................................................................ 44

2-11. Choosing a View Engine .....................................................................................................45

Problem ............................................................................................................................................................... 45

Solution................................................................................................................................................................ 46

How It Works ........................................................................................................................................................ 46

2-12. Understanding Razor Syntax ..............................................................................................49

Problem ............................................................................................................................................................... 49

Solution................................................................................................................................................................ 49

How It Works ........................................................................................................................................................ 49

Page 8: ASP.NET MVC 4 Recipes - link.springer.com3A978-1-4302-4774-6%2F1.pdf · ISBN 978-1-4302-4774-6 (eBook) This book is dedicated to all of the developers who stay up to 3AM for weeks

CONTENTS

x

2-13. Installing an Alternative View Engine .................................................................................53Problem ............................................................................................................................................................... 53

Solution................................................................................................................................................................ 53

How It Works ........................................................................................................................................................ 53

Chapter 3: Setting Up Your Environment .........................................................................59

3-1. Acquiring the Ideal Developer Hardware .............................................................................59Problem ............................................................................................................................................................... 59

Solution................................................................................................................................................................ 59

How It Works ........................................................................................................................................................ 59

3-2. Choosing an Operating System for Your Development Machine ..........................................63Problem ............................................................................................................................................................... 63

Solution................................................................................................................................................................ 63

How It Works ........................................................................................................................................................ 63

3-3. Choosing Between IIS Express, IIS 8, and the Visual Studio Development Server ...............64Problem ............................................................................................................................................................... 64

Solution................................................................................................................................................................ 64

How It Works ........................................................................................................................................................ 64

3-4. Filling Your Development Toolbox ........................................................................................66Problem ............................................................................................................................................................... 66

Solution................................................................................................................................................................ 66

3-5. Installing Hyper-V and Setting Up a Virtual Machine ...........................................................67Problem ............................................................................................................................................................... 67

Solution................................................................................................................................................................ 67

How It Works ........................................................................................................................................................ 68

3-6. Connecting to a Remote Development Machine Using RDP ................................................79Problem ............................................................................................................................................................... 79

Solution................................................................................................................................................................ 79

How It Works ........................................................................................................................................................ 79

3-7. Installing Visual Studio 2012 ................................................................................................80Problem ............................................................................................................................................................... 80

Solution................................................................................................................................................................ 80

How It Works ........................................................................................................................................................ 80

Page 9: ASP.NET MVC 4 Recipes - link.springer.com3A978-1-4302-4774-6%2F1.pdf · ISBN 978-1-4302-4774-6 (eBook) This book is dedicated to all of the developers who stay up to 3AM for weeks

CONTENTS

xi

3-8. Installing Visual Studio MVC 4 Add-on for Visual Studio 2010 .............................................82

Problem ............................................................................................................................................................... 82

Solution................................................................................................................................................................ 82

How It works ........................................................................................................................................................ 82

3-9. Installing IIS .........................................................................................................................82

Problem ............................................................................................................................................................... 82

Solution................................................................................................................................................................ 82

How It Works ........................................................................................................................................................ 83

3-10. Configure Your MVC Project to Use IIS Rather Than IIS Express ........................................91

Problem ............................................................................................................................................................... 91

Solution................................................................................................................................................................ 91

How It Works ........................................................................................................................................................ 91

3-11. Configure an Application Pool to Use an Application Identity ............................................98

Problem ............................................................................................................................................................... 98

Solution................................................................................................................................................................ 99

How It Works ........................................................................................................................................................ 99

3-12. Configuring Visual Studio to Automatically Launch As Administrator ..............................101

Problem ............................................................................................................................................................. 101

Solution.............................................................................................................................................................. 101

How It Works ...................................................................................................................................................... 101

3-13. Setting Up a Shared Instance of LocalDB ........................................................................102

Problem ............................................................................................................................................................. 102

Solution.............................................................................................................................................................. 102

How It works ...................................................................................................................................................... 102

Chapter 4: Visual Studio 2012 Overview .......................................................................105

4-1. Understanding Visual Studio 2012 .....................................................................................105

Problem ............................................................................................................................................................. 105

Solution.............................................................................................................................................................. 105

How It Works ...................................................................................................................................................... 105

Page 10: ASP.NET MVC 4 Recipes - link.springer.com3A978-1-4302-4774-6%2F1.pdf · ISBN 978-1-4302-4774-6 (eBook) This book is dedicated to all of the developers who stay up to 3AM for weeks

CONTENTS

xii

4-2. Changing the Style of the Visual Studio Menu ...................................................................115

Problem ............................................................................................................................................................. 115

Solution.............................................................................................................................................................. 115

How It Works ...................................................................................................................................................... 115

4-3. Changing the Visual Studio Color Scheme .........................................................................116

Problem ............................................................................................................................................................. 116

Solution.............................................................................................................................................................. 116

How It works ...................................................................................................................................................... 117

4-4. Installing Visual Studio Documentation for Offline Use ......................................................117

Problem ............................................................................................................................................................. 117

Solution.............................................................................................................................................................. 117

4-5. Resetting the Default Visual Studio Window Layout ..........................................................118

Problem ............................................................................................................................................................. 118

Solution.............................................................................................................................................................. 118

How It works ...................................................................................................................................................... 119

4-6. Creating a Custom Toolbar .................................................................................................119

Problem ............................................................................................................................................................. 119

Solution.............................................................................................................................................................. 119

How It Works ...................................................................................................................................................... 119

4-7. Navigating a Large Razor View or HTML File .....................................................................120

Problem ............................................................................................................................................................. 120

Solution.............................................................................................................................................................. 120

How It Works ...................................................................................................................................................... 121

4-8. Using the Page Inspector to Troubleshoot CSS Layout Bugs .............................................121

Problem ............................................................................................................................................................. 121

Solution.............................................................................................................................................................. 122

How It Works ...................................................................................................................................................... 122

4-9. Using the Page Inspector While Debugging .......................................................................125

Problem ............................................................................................................................................................. 125

Solution.............................................................................................................................................................. 126

How It Works ...................................................................................................................................................... 126

Page 11: ASP.NET MVC 4 Recipes - link.springer.com3A978-1-4302-4774-6%2F1.pdf · ISBN 978-1-4302-4774-6 (eBook) This book is dedicated to all of the developers who stay up to 3AM for weeks

CONTENTS

xiii

4-10. Understanding Visual Studio’s Debugging Windows ........................................................126

Problem ............................................................................................................................................................. 126

Solution.............................................................................................................................................................. 127

How It Works ...................................................................................................................................................... 127

4-11. Setting a Conditional Breakpoint .....................................................................................129

Problem ............................................................................................................................................................. 129

Solution.............................................................................................................................................................. 129

How It works ...................................................................................................................................................... 130

4-12. Using the Output Window ................................................................................................131

Problem ............................................................................................................................................................. 131

Solution.............................................................................................................................................................. 131

How It Works ...................................................................................................................................................... 131

4-13. Using the Extension Manager to Find and Install Add-ons for Visual Studio ...................134

Problem ............................................................................................................................................................. 134

Solution.............................................................................................................................................................. 134

How It Works ...................................................................................................................................................... 134

4-14. Visual Studio Keyboard Shortcuts ....................................................................................136

Problem ............................................................................................................................................................. 136

Solution.............................................................................................................................................................. 136

How It Works ...................................................................................................................................................... 136

Chapter 5: Getting the Most from the Built-in Templates ..............................................139

5-1. Choosing an ASP.NET MVC 4 Project Template ..................................................................139

Problem ............................................................................................................................................................. 139

Solution.............................................................................................................................................................. 139

How It Works ...................................................................................................................................................... 139

5-2. Creating a Simple Web Site Using the ASP.NET MVC 4 Empty Template ............................147

Problem ............................................................................................................................................................. 147

Solution.............................................................................................................................................................. 147

How It Works ...................................................................................................................................................... 148

Page 12: ASP.NET MVC 4 Recipes - link.springer.com3A978-1-4302-4774-6%2F1.pdf · ISBN 978-1-4302-4774-6 (eBook) This book is dedicated to all of the developers who stay up to 3AM for weeks

CONTENTS

xiv

5-3. Creating a Simple Form with Validation Using the ASP.NET MVC 4 Basic Template ..........151

Problem ............................................................................................................................................................. 151

Solution.............................................................................................................................................................. 151

How It Works ...................................................................................................................................................... 152

5-4. Creating an Intranet Site That Uses Windows Authentication Using the ASP.NET MVC 4 Intranet Template ....................................................................................................161

Problem ............................................................................................................................................................. 161

Solution.............................................................................................................................................................. 161

How It Works ...................................................................................................................................................... 161

5-5. Configuring Windows Authentication Using the Web.config File ........................................162

Problem ............................................................................................................................................................. 162

Solution.............................................................................................................................................................. 162

How It Works ...................................................................................................................................................... 163

5-6. Creating an Internet Site That Uses Forms Authentication Using the Internet Application Template..........................................................................................................164

Problem ............................................................................................................................................................. 164

Solution.............................................................................................................................................................. 164

How It Works ...................................................................................................................................................... 165

5-7. Modifying an Internet Site to Use a Custom SQL Server Database ....................................171

Problem ............................................................................................................................................................. 171

Solution.............................................................................................................................................................. 171

How It Works ...................................................................................................................................................... 172

5-8. Customizing the Registration Page on an Internet Site Created with the ASP.NET MVC 4 Internet Template ....................................................................................................177

Problem ............................................................................................................................................................. 177

Solution.............................................................................................................................................................. 177

How It Works ...................................................................................................................................................... 178

Chapter 6: Architecting Applications with ASP.NET MVC ...............................................185

6-1. Understanding the Role of the Architect ............................................................................185

Problem ............................................................................................................................................................. 185

Solution.............................................................................................................................................................. 185

How It Works ...................................................................................................................................................... 186

Page 13: ASP.NET MVC 4 Recipes - link.springer.com3A978-1-4302-4774-6%2F1.pdf · ISBN 978-1-4302-4774-6 (eBook) This book is dedicated to all of the developers who stay up to 3AM for weeks

CONTENTS

xv

6-2. Understanding the Buy or Build Decision ..........................................................................187

Problem ............................................................................................................................................................. 187

Solution.............................................................................................................................................................. 187

How It Works ...................................................................................................................................................... 188

6-3. Deciding Between a Public Cloud and a Private Data Center ............................................190

Problem ............................................................................................................................................................. 190

Solution.............................................................................................................................................................. 190

How It Works ...................................................................................................................................................... 191

6-4. Determining the Size of Your Application’s Local Network ................................................192

Problem ............................................................................................................................................................. 192

Solution.............................................................................................................................................................. 192

How It Works ...................................................................................................................................................... 192

6-5. Determining Which Operating System to Deploy ...............................................................194

Problem ............................................................................................................................................................. 194

Solution.............................................................................................................................................................. 194

How It Works ...................................................................................................................................................... 194

6-6. Documenting Your Architecture .........................................................................................195

Problem ............................................................................................................................................................. 195

Solution.............................................................................................................................................................. 195

How It Works ...................................................................................................................................................... 195

6-7. Architecting an ASP.NET MVC Application for the Internet .................................................200

Problem ............................................................................................................................................................. 200

Solution.............................................................................................................................................................. 200

How It Works ...................................................................................................................................................... 200

6-8. Architecting a Large-Scale Internet Application ................................................................204

Problem ............................................................................................................................................................. 204

Solution.............................................................................................................................................................. 204

How It Works ...................................................................................................................................................... 205

6-9. Architecting a Line-of-Business Application with ASP.NET MVC ........................................208

Problem ............................................................................................................................................................. 208

Solution.............................................................................................................................................................. 208

How It Works ...................................................................................................................................................... 208

Page 14: ASP.NET MVC 4 Recipes - link.springer.com3A978-1-4302-4774-6%2F1.pdf · ISBN 978-1-4302-4774-6 (eBook) This book is dedicated to all of the developers who stay up to 3AM for weeks

CONTENTS

xvi

6-10. Configuring SQL Server to Use a Custom Port .................................................................211

Problem ............................................................................................................................................................. 211

Solution.............................................................................................................................................................. 212

How It Works ...................................................................................................................................................... 212

Chapter 7: Solution Design ............................................................................................215

7-1. Designing a View That Requires Data from Multiple Models .............................................215

Problem ............................................................................................................................................................. 215

Solution.............................................................................................................................................................. 215

How It Works ...................................................................................................................................................... 215

7-2. Using Partial Views to Construct a Composite User Interface............................................223

Problem ............................................................................................................................................................. 223

Solution.............................................................................................................................................................. 223

How It Works ...................................................................................................................................................... 223

7-3. Creating a Model Using Entity Framework Database First ................................................235

Problem ............................................................................................................................................................. 235

Solution.............................................................................................................................................................. 236

How It Works ...................................................................................................................................................... 236

7-4. Creating a Model Using Entity Framework Database First with Oracle .............................242

Problem ............................................................................................................................................................. 242

Solution.............................................................................................................................................................. 242

How It Works ...................................................................................................................................................... 242

7-5. Creating a Model Using Entity Framework Model First ......................................................247

Problem ............................................................................................................................................................. 247

Solution.............................................................................................................................................................. 247

How It Works ...................................................................................................................................................... 247

7-6. Creating a Model Using Entity Framework Code First .......................................................253

Problem ............................................................................................................................................................. 253

Solution.............................................................................................................................................................. 253

How It Works ...................................................................................................................................................... 253

Page 15: ASP.NET MVC 4 Recipes - link.springer.com3A978-1-4302-4774-6%2F1.pdf · ISBN 978-1-4302-4774-6 (eBook) This book is dedicated to all of the developers who stay up to 3AM for weeks

CONTENTS

xvii

7-7. Improving Application Startup Performance ......................................................................264

Problem ............................................................................................................................................................. 264

Solution.............................................................................................................................................................. 264

How It Works ...................................................................................................................................................... 264

7-8. Encrypting the Connection Strings in Your Web.config File ...............................................265

Problem ............................................................................................................................................................. 265

Solution.............................................................................................................................................................. 265

How It Works ...................................................................................................................................................... 265

7-9. Using Areas to Organize a Large ASP.MVC Project .............................................................266

Problem ............................................................................................................................................................. 266

Solution.............................................................................................................................................................. 266

How It Works ...................................................................................................................................................... 267

7-10. Disabling Automatic Table Creation with the Simple Membership Provider ....................272

Problem ............................................................................................................................................................. 272

Solution.............................................................................................................................................................. 272

How It Works ...................................................................................................................................................... 272

Chapter 8: Asynchronous Programming with ASP.NET MVC .........................................281

8-1. Using Async Actions ...........................................................................................................281

Problem ............................................................................................................................................................. 281

Solution.............................................................................................................................................................. 281

How It Works ...................................................................................................................................................... 281

8-2. Running Several Asynchronous Calls in Parallel Inside an Action Method ........................283

Problem ............................................................................................................................................................. 283

Solution.............................................................................................................................................................. 283

How It Works ...................................................................................................................................................... 284

8-3. Consuming WCF Service from an MVC 4 Project ...............................................................286

Problem ............................................................................................................................................................. 286

Solution.............................................................................................................................................................. 286

How It Works ...................................................................................................................................................... 286

Page 16: ASP.NET MVC 4 Recipes - link.springer.com3A978-1-4302-4774-6%2F1.pdf · ISBN 978-1-4302-4774-6 (eBook) This book is dedicated to all of the developers who stay up to 3AM for weeks

CONTENTS

xviii

8-4. Boosting the Performance of Task-Based Asynchronous Communications Using Caching ....................................................................................................................290

Problem ............................................................................................................................................................. 290

Solution.............................................................................................................................................................. 290

How It Works ...................................................................................................................................................... 290

8-5. Using IL Disassembler to See Code Generated by the C# Compiler ...................................298

Problem ............................................................................................................................................................. 298

Solution.............................................................................................................................................................. 298

How It Works ...................................................................................................................................................... 298

8-6. Understanding Threading in IIS ..........................................................................................304

Problem ............................................................................................................................................................. 304

Solution.............................................................................................................................................................. 304

How It Works ...................................................................................................................................................... 304

8-7. Using Internet Information Services (IIS) Request Monitoring and Tracing .......................315

Problem ............................................................................................................................................................. 315

Solution.............................................................................................................................................................. 316

How It Works ...................................................................................................................................................... 316

Chapter 9: Test-Driven Development with ASP.NET MVC 4 ............................................321

9-1. Using Testing to Improve the Quality of Your ASP.NET MVC Application .............................321

Problem ............................................................................................................................................................. 321

Solution.............................................................................................................................................................. 321

How It Works ...................................................................................................................................................... 321

9-2. Creating Unit Tests for a Controller Action Using MS Test ..................................................325

Problem ............................................................................................................................................................. 325

Solution.............................................................................................................................................................. 325

How It Works ...................................................................................................................................................... 325

9-3. Understanding Test-Driven Development Strategies .........................................................333

Problem ............................................................................................................................................................. 333

Solution.............................................................................................................................................................. 333

How It Works ...................................................................................................................................................... 333

Page 17: ASP.NET MVC 4 Recipes - link.springer.com3A978-1-4302-4774-6%2F1.pdf · ISBN 978-1-4302-4774-6 (eBook) This book is dedicated to all of the developers who stay up to 3AM for weeks

CONTENTS

xix

9-4. Defining a Naming Convention for Your Unit Tests .............................................................348

Problem ............................................................................................................................................................. 348

Solution.............................................................................................................................................................. 348

How It Works ...................................................................................................................................................... 348

9-5. Selecting a Unit Test Framework .......................................................................................349

Problem ............................................................................................................................................................. 349

Solution.............................................................................................................................................................. 349

How It Works ...................................................................................................................................................... 349

9-6. Downloading and Installing NUnit ......................................................................................355

Problem ............................................................................................................................................................. 355

Solution.............................................................................................................................................................. 355

How It Works ...................................................................................................................................................... 355

9-7. Creating Unit Tests for Controller Actions Using NUnit .......................................................356

Problem ............................................................................................................................................................. 356

Solution.............................................................................................................................................................. 356

How It Works ...................................................................................................................................................... 356

9-8. Mocking a Repository with Moq ........................................................................................358

Problem ............................................................................................................................................................. 358

Solution.............................................................................................................................................................. 358

How It Works ...................................................................................................................................................... 358

Chapter 10: Moving From Web Forms to ASP.NET MVC .................................................375

10-1. Creating a Simple Data List Using ASP.NET MVC .............................................................375

Problem ............................................................................................................................................................. 375

Solution.............................................................................................................................................................. 375

How It Works ...................................................................................................................................................... 375

10-2. Creating a Multiple Column Data List Using a Custom HTML Helper Extension ..............391

Problem ............................................................................................................................................................. 391

Solution.............................................................................................................................................................. 391

How It Works ...................................................................................................................................................... 391

Page 18: ASP.NET MVC 4 Recipes - link.springer.com3A978-1-4302-4774-6%2F1.pdf · ISBN 978-1-4302-4774-6 (eBook) This book is dedicated to all of the developers who stay up to 3AM for weeks

CONTENTS

xx

10-3. Creating a Data Grid with Paging, Sorting, and Filtering Support ....................................403

Problem ............................................................................................................................................................. 403

Solution.............................................................................................................................................................. 403

How It Works ...................................................................................................................................................... 403

10-4. Creating a Data Grid That Allows Inline Editing ................................................................424

Problem ............................................................................................................................................................. 424

Solution.............................................................................................................................................................. 424

How It Works ...................................................................................................................................................... 425

10-5. Creating a Master/Details View in ASP.NET MVC..............................................................431

Problem ............................................................................................................................................................. 431

Solution.............................................................................................................................................................. 431

How It Works ...................................................................................................................................................... 431

10-6. Custom Validators in ASP.NET MVC ..................................................................................437

Problem ............................................................................................................................................................. 437

Solution.............................................................................................................................................................. 437

How It Works ...................................................................................................................................................... 438

10-7. Moving from Master Pages in ASP.NET Web Forms to Layout Pages in Razor .................442

Problem ............................................................................................................................................................. 442

Solution.............................................................................................................................................................. 442

How It Works ...................................................................................................................................................... 442

10-8. Creating a Multipage Wizard Using ASP.NET MVC ............................................................444

Problem ............................................................................................................................................................. 444

Solution.............................................................................................................................................................. 444

How It Works ...................................................................................................................................................... 444

10-9. Adding MVC to a Web Forms Project ................................................................................463

Problem ............................................................................................................................................................. 463

Solution.............................................................................................................................................................. 464

How It Works ...................................................................................................................................................... 464

Page 19: ASP.NET MVC 4 Recipes - link.springer.com3A978-1-4302-4774-6%2F1.pdf · ISBN 978-1-4302-4774-6 (eBook) This book is dedicated to all of the developers who stay up to 3AM for weeks

CONTENTS

xxi

Chapter 11: Creating Modern User Experiences Using jQuery, Knockout.js, and Web API .....475

11-1. Creating an Adaptive Multicolumn Layout Using CSS Media Queries ..............................475

Problem ............................................................................................................................................................. 475

Solution.............................................................................................................................................................. 475

How It Works ...................................................................................................................................................... 475

11-2. Creating a Data Grid That Can Page and Sort Without Full-Page Postbacks ...................482

Problem ............................................................................................................................................................. 482

Solution.............................................................................................................................................................. 482

How It Works ...................................................................................................................................................... 482

11-3. Implementing Two-Way Data Binding Using Web API and Knockout ...............................504

Problem ............................................................................................................................................................. 504

Solution.............................................................................................................................................................. 504

How It Works ...................................................................................................................................................... 505

11-4. Creating a Custom Route for an API Controller ................................................................533

Problem ............................................................................................................................................................. 533

Solution.............................................................................................................................................................. 533

How It Works ...................................................................................................................................................... 533

11-5. Using Fiddler to Debug a Web API ....................................................................................539

Problem ............................................................................................................................................................. 539

Solution.............................................................................................................................................................. 539

How It Works ...................................................................................................................................................... 539

Chapter 12: Mobile, Social, and Cloud Technologies .....................................................543

12-1. Using Facebook As Your MVC Web Site’s Identity Provider ..............................................543

Problem ............................................................................................................................................................. 543

Solution.............................................................................................................................................................. 543

How It Works ...................................................................................................................................................... 543

12-2. Creating a Facebook Canvas Page Application with ASP.NET MVC ..................................551

Problem ............................................................................................................................................................. 551

Solution.............................................................................................................................................................. 552

How It Works ...................................................................................................................................................... 552

Page 20: ASP.NET MVC 4 Recipes - link.springer.com3A978-1-4302-4774-6%2F1.pdf · ISBN 978-1-4302-4774-6 (eBook) This book is dedicated to all of the developers who stay up to 3AM for weeks

CONTENTS

xxii

12-3. Setting Up Your Development for Working with Windows Azure ......................................560

Problem ............................................................................................................................................................. 560

Solution.............................................................................................................................................................. 560

How It Works ...................................................................................................................................................... 560

12-4. Storing and Retrieving Files on Windows Azure from an ASP.NET MVC 4 Application ............561

Problem ............................................................................................................................................................. 561

Solution.............................................................................................................................................................. 561

How It Works ...................................................................................................................................................... 562

12-5. Using Fiddler 2 to Help Debug Azure Calls to the Storage Emulator ................................573

Problem ............................................................................................................................................................. 573

Solution.............................................................................................................................................................. 573

How It Works ...................................................................................................................................................... 573

12-6. Which Is Better: a Mobile Web Application or a Native Device Application? ....................574

Problem ............................................................................................................................................................. 574

Solution.............................................................................................................................................................. 574

How It Works ...................................................................................................................................................... 574

12-7. Creating a Mobile Web Application Using jQuery Mobile and ASP.NET MVC 4 .................578

Problem ............................................................................................................................................................. 578

Solution.............................................................................................................................................................. 578

How It Works ...................................................................................................................................................... 578

12-8. Testing Your Mobile Web Application ...............................................................................581

Problem ............................................................................................................................................................. 581

Solution.............................................................................................................................................................. 581

How It Works ...................................................................................................................................................... 581

Index .................................................................................................................................589

Page 21: ASP.NET MVC 4 Recipes - link.springer.com3A978-1-4302-4774-6%2F1.pdf · ISBN 978-1-4302-4774-6 (eBook) This book is dedicated to all of the developers who stay up to 3AM for weeks

xxiii

About the Author

John Ciliberti is an enterprise software architect who has been designing and developing web applications and enterprise scale solutions for more than 15 years. John has been creating web applications using Microsoft technologies since the early days of classic ASP development in the late 1990s. In addition to his work as a consultant and software architect, John contributes to several open-source projects and is the founder of the MyOnlineBand.com online musician collaboration community.

He is currently employed at KPMG LLC in Montvale, New Jersey, as an associate director in the KPMG US Enterprise Architecture group. In his role as an enterprise architect, John helps guide the firm’s strategic technical direction and advises IT leadership on major technical initiatives. John also works closely with project teams by providing initial architectural design and architectural guidance and technical assistance throughout the project.

Page 22: ASP.NET MVC 4 Recipes - link.springer.com3A978-1-4302-4774-6%2F1.pdf · ISBN 978-1-4302-4774-6 (eBook) This book is dedicated to all of the developers who stay up to 3AM for weeks

xxv

About the Technical Reviewer

Damien Foggon is a developer, writer, and technical reviewer in cutting-edge technologies. He has contributed to more than 50 books on .NET, C#, Visual Basic, and ASP.NET. He is the co-founder of the Newcastle-based user group NEBytes (www.nebytes.net) and he is a multiple MCPD in .NET 2.0 and .NET 3.5. Damien can be found online at http://blog.fasm.co.uk.

Page 23: ASP.NET MVC 4 Recipes - link.springer.com3A978-1-4302-4774-6%2F1.pdf · ISBN 978-1-4302-4774-6 (eBook) This book is dedicated to all of the developers who stay up to 3AM for weeks

xxvii

This book would not be possible without the help and understanding of many people. At the top of my list is my wife Kathy for putting up with me leaving her alone while I stayed up until 2 a.m. coding and writing for nights on end. I would like to thank my kids Katrina and Maria for forgiving all the missed playdates and bedtime stories. I would also like to thank my old friend Greg Orsulak for allowing me to reference our early computer inspirations as part of the book’s introduction.

I would like to thank my technical reviewer Damien Foggon and the staff at Apress, especially Tom Welsh, for all their hard work and attention to detail.

I cannot forget my colleagues at KPMG, especially Chris Marshall and Mike Soranno, who have supported and encouraged me throughout this long and intense process.

Last but not least, I would like to thank Fareed Shaikh, whose philosophy lessons at Taco Bell have raised both my state of consciousness and my cholesterol.

Acknowledgments

Page 24: ASP.NET MVC 4 Recipes - link.springer.com3A978-1-4302-4774-6%2F1.pdf · ISBN 978-1-4302-4774-6 (eBook) This book is dedicated to all of the developers who stay up to 3AM for weeks

xxix

Introduction

The first time I saw a computer that could connect to a network was back in the late ’80s when my friend Greg showed me how we could use his Apple IIe to download the Anarchy Cookbook from a bulletin board service. That first experience, which included a misguided crash course in chemistry, eventually led Greg to a career as a pharmacist and got me hooked on the potential of computers.

Fast-forward to 2013 and the Internet is everywhere. It’s in your home, at your job, in your car, and in your pocket. You can access the Web from your PC, your phone, your music player, your tablet, and you can even stream movies to your TV. Terrestrial radio is gradually being replaced by Internet music services, cable TV by services from Netflix and Amazon, paper maps by GPS systems, magazines and newspapers by e-readers and tablets. You surf the Web using touch screens, your voice, and TV remotes. Web pages adapt to fit your iPad’s orientation, your screen dims automatically to account for room lighting, your game console and your phone can recognize your face, and your tablet can read your handwriting.

As software developers, you need to be three steps ahead of this changing world. You are on the front lines. The world expects that not only will your software work but also that it will get smarter, faster, more reliable, and easier to use. These goals are only possible with improved techniques and better tools.

Adopting new techniques and learning new tools can be challenging, especially when you are on a tight schedule. When learning new technologies, many developers can spend more time searching Google and reading blogs and forum posts than writing code. Many times, they end up using a code snippet from some random source without a full understanding of what it does. The code may seem to fit the need at that time but if it ever breaks, they do not know how to fix it because they don’t understand it.

This book was written to help ASP.NET MVC developers like you to quickly find the code you need to move your project forward. More importantly, this book also will help you understand how each solution works. Each solution is broken down step by step and each code sample is explained in detail. In many cases, the explanation will go beyond the code and will discuss what is happening behind the scenes.