Jason N. Gaylord
Christian Wenz
Pranav Rastogi
Todd Miranda
Scott Hanselman

Professional ASP.NET 4.5 in C# and VB
Published by
John Wiley & Sons, Inc.
10475 Crosspoint Boulevard
Indianapolis, IN 46256
www.wiley.com
Copyright © 2013 by John Wiley & Sons, Inc., Indianapolis, Indiana
Published simultaneously in Canada
ISBN: 978-1-118-31182-0
ISBN: 978-1-118-42438-4 (ebk)
ISBN: 978-1-118-33205-4 (ebk)
ISBN: 978-1-118-33534-5 (ebk)
Manufactured in the United States of America
10 9 8 7 6 5 4 3 2 1
No part of this publication may be reproduced, stored in a retrieval system or transmitted in any form or by any means, electronic, mechanical, photocopying, recording, scanning or otherwise, except as permitted under Sections 107 or 108 of the 1976 United States Copyright Act, without either the prior written permission of the Publisher, or authorization through payment of the appropriate per-copy fee to the Copyright Clearance Center, 222 Rosewood Drive, Danvers, MA 01923, (978) 750-8400, fax (978) 646-8600. Requests to the Publisher for permission should be addressed to the Permissions Department, John Wiley & Sons, Inc., 111 River Street, Hoboken, NJ 07030, (201) 748-6011, fax (201) 748-6008, or online at http://www.wiley.com/go/permissions.
Limit of Liability/Disclaimer of Warranty: The publisher and the author make no representations or warranties with respect to the accuracy or completeness of the contents of this work and specifically disclaim all warranties, including without limitation warranties of fitness for a particular purpose. No warranty may be created or extended by sales or promotional materials. The advice and strategies contained herein may not be suitable for every situation. This work is sold with the understanding that the publisher is not engaged in rendering legal, accounting, or other professional services. If professional assistance is required, the services of a competent professional person should be sought. Neither the publisher nor the author shall be liable for damages arising herefrom. The fact that an organization or Web site is referred to in this work as a citation and/or a potential source of further information does not mean that the author or the publisher endorses the information the organization or Web site may provide or recommendations it may make. Further, readers should be aware that Internet Web sites listed in this work may have changed or disappeared between when this work was written and when it is read.
For general information on our other products and services please contact our Customer Care Department within the United States at (877) 762-2974, outside the United States at (317) 572-3993 or fax (317) 572-4002.
Wiley publishes in a variety of print and electronic formats and by print-on-demand. Some material included with standard print versions of this book may not be included in e-books or in print-on-demand. If this book refers to media such as a CD or DVD that is not included in the version you purchased, you may download this material at http://booksupport.wiley.com. For more information about Wiley products, visit www.wiley.com.
Library of Congress Control Number: 2013933610
Trademarks: Wiley, Wrox, the Wrox logo, Wrox Programmer to Programmer, and related trade dress are trademarks or registered trademarks of John Wiley & Sons, Inc. and/or its affiliates, in the United States and other countries, and may not be used without written permission. All other trademarks are the property of their respective owners. John Wiley & Sons, Inc., is not associated with any product or vendor mentioned in this book.
To my loving wife, Lisa, and our four awesome children.
—Jason N. Gaylord
I would like to dedicate this book with the following quote, to my mother who has been instrumental in my upbringing and is an integral part of my life. Abraham Lincoln said, “All that I am, or hope to be, I owe to my angel mother.”
—Pranav Rastogi
To Kelly, Amber, and Sydney
—Todd Miranda
To my wife and the two little boys I work for.
—Scott Hanselman
JASON N. GAYLORD is an application developer and Director of Technology at United One Resources in Wilkes-Barre, Pennsylvania. His programming career started with a simple GOTO statement and continued over 15 years of developing Windows and web applications. During this time, Jason has had the opportunity to plan, build, test, and deploy several enterprise applications including integrations with accounting and operating systems and B2B web applications used by some of the top banks in the United States.
Outside of the normal day to day activities, Jason blogs, speaks, and organizes technology events including TECHbash and NEPA GiveCamp. He’s the group leader for the .NET Valley technology user group. Recently, Jason was recognized as a Microsoft MVP for 10 consecutive years. He remains a member of the ASPInsiders, a group that provides product feedback directly to the ASP.NET team. You can find out more about Jason by visiting his website at jasongaylord.com. You can follow him on Twitter at @jgaylord.
CHRISTIAN WENZ started working almost exclusively with web technologies in 1993 and has not looked back since. As a developer and project manager he has been responsible for websites from medium–sized companies up to large, international enterprises. As an author, he has written or co-written over 100 books that have been translated into 10 languages. As a consultant, he helped companies and teams of developers to make their applications more reliable, better performing, and more secure. Christian frequently writes for various IT magazines, is a sought-after speaker at developer conferences around the world, and is always keen on sharing technologies he is excited about with others. He contributes to various open source projects, is a Microsoft ASP.NET MVP and an ASPInsider, and co-author of several developer certifications. You can follow him on Twitter at @chwenz.
PRANAV RASTOGI is a member of the Microsoft ASP.NET product team and is based in Seattle. Over the past years, Pranav has worked on a variety of areas such as ASP.NET Web Forms, Dynamic Data, and more recently ASP.NET SignalR. He is passionate about understanding web developer woes with the Microsoft stack and helps champion tools, packages, or libraries that will enhance the web development experience on the Microsoft stack. Pranav is also a regular speaker on a variety of ASP.NET related topics at various worldwide conferences. He blogs about his experiences at http://blogs.msdn.com/b/pranav_rastogi/. Before joining Microsoft, Pranav graduated from the University Of Florida with a masters in Computer Science.
Pranav is also a food junkie who sometimes works as a Chef de Cuisine at home. He is an adventure seeker and can be often found backpacking in the wilderness. In this spare time Pranav regularly practices his bartending skills on his friends. If you know anyone who is looking to throw a party and needs a bartender, then get in touch with him. You can follow Pranav on Twitter at @rustd.
TODD MIRANDA is an active proponent of .NET technologies and software craftsmanship as a whole. He has been developing on various platforms for over 20 years and has been involved with .NET since it was previewed in 2000. He has worked with many of the .NET platforms but focuses primarily on ASP.NET. He is the owner of NxtDimension Solutions and provides consulting and training on the Microsoft stack. As a Microsoft MVP, Todd works closely with Microsoft. He is the co-founder of the Birmingham .NET User Group and remains active in the development community. He has recorded hundreds of online training videos on Microsoft development topics including security, JavaScript, Silverlight, WPF, Expression, and ASP.NET. He is an INETA speaker and regularly presents at user groups, community events, and technical conferences. You can follow Todd on Twitter @tmiranda.
SCOTT HANSELMAN is a web developer who has been blogging at http://hanselman.com for over a decade. He works on Azure and ASP.NET for Microsoft out of his home office in Portland, Oregon. Scott has three podcasts, http://hanselminutes.com for tech talk, http://thisdeveloperslife.com on developers’ lives and loves, and http://ratchetandthegeek.com for pop culture and tech media. He’s written a number of books and spoken in person to almost a half million developers worldwide. You can follow Scott on Twitter at @shanselman.
KEN COX has been creating web applications with Microsoft technologies since the earliest betas of ASP Classic in the mid-1990s. He has worked on dozens of ASP.NET sites, large and small, and is the author of ASP.NET 3.5 For Dummies published by John Wiley & Sons. Ken was honored as a Microsoft Most Valuable Professional (MVP) for ASP.NET for 15 consecutive years prior to his retirement and remains an ASPInsider. Ken does consulting work from his home in Nipissing Township, Ontario, Canada.
BIPIN JOSHI is an independent blogger and author who writes about apparently unrelated topics — yoga and technology. A former software consultant and trainer by profession, Bipin has been programming since 1995 and has worked with the .NET Framework since its inception. He is a published author and has authored or co-authored more than a half dozen books and numerous articles on .NET technologies. Bipin was a Microsoft Most Valuable Professional (MVP) and a Microsoft Certified Trainer (MCT) during his tenure as a software consultant and trainer. He has also penned a few books on yoga. Having embraced the yoga way of life, he enjoys the intoxicating presence of God and writes about yoga, life, and technology. He can be reached at www.bipinjoshi.com.
JOHN PETERSEN, an early adopter of the Microsoft .NET Platform, has over 20 years of experience architecting and developing software. Currently, John is a practice director in the Technology Platform Group at Neudesic, LLC, a Microsoft National Systems Integrator and Gold ISV Partner and is a 10-time recipient of Microsoft’s Most Valuable Professional Award. John is a current ASP.NET/IIS MVP. John has spoken at numerous industry events, including VSLive and DevConnections, as well as at many regional events and Code Camps. John is also a regular contributor to CODE Magazine.
JEFFERY TAY has been developing and designing .NET solutions since 2002, specializing in solutions for the education and medical sectors. He was awarded Microsoft Most Valuable Professional for the last 3 years. He has experience in Java and most Microsoft technologies such as Silverlight, SQL Server, and Windows Server. He is an associate director at the National University of Singapore where he leads a team that manages the university’s Learning Management System and comes up with new solutions where the use of IT can enhance teaching. You can reach Jeffery at taykwama@hotmail.com.
DEEPAK VERMA has been developing and designing web solutions for the last 10 years. During these years, Deepak has been involved in technologies such as Flash Scripting, Flex, ColdFusion, Silverlight, Java, and PHP and now is mostly working on Microsoft stack. He is currently working as a Software Development Engineer in Test for Microsoft. At Microsoft he has contributed to Visual Studio Dynamic data tooling, multi-targeting, Razor editor and tooling, Azure SDK for Java, PHP & .NET, Visual Studio HTML and CSS editors, Azure Mobile Services, and NuGet. He lives in Redmond, Washington, with his wife, Madhu, and children, Adya and Kian. You can reach him at deepu_verma@yahoo.com.
ACQUISITIONS EDITOR
Mary James
SENIOR PROJECT EDITOR
Kevin Kent
TECHNICAL EDITORS
Ken Cox
Bipin Joshi
John Petersen
Jeffery Tay
Deepak Verma
SENIOR PRODUCTION EDITOR
Kathleen Wisor
COPY EDITORS
Kim Cofer
Kezia Endsley
EDITORIAL MANAGER
Mary Beth Wakefield
FREELANCER EDITORIAL MANAGER
Rosemarie Graham
ASSOCIATE DIRECTOR OF MARKETING
David Mayhew
MARKETING MANAGER
Ashley Zurcher
BUSINESS MANAGER
Amy Knies
PRODUCTION MANAGER
Tim Tate
VICE PRESIDENT AND EXECUTIVE GROUP PUBLISHER
Richard Swadley
VICE PRESIDENT AND EXECUTIVE PUBLISHER
Neil Edde
ASSOCIATE PUBLISHER
Jim Minatel
PROJECT COORDINATOR, COVER
Katie Crocker
PROOFREADER
Nancy Carrasco
INDEXER
Robert Swanson
COVER DESIGNER
Elizabeth Brooks
COVER IMAGE
© Marilyn Nieves / iStockphoto
BACKGROUND
© PhotoAlto Images/Fotosearch
I’D LIKE TO THANK the time and dedication of the team at Wrox Press and John Wiley & Sons. Without their support, this book would not be possible. Thanks to Scott Hunter, Scott Hanselman, Damian Edwards, and the rest of the ASP.NET team for their current leadership to bring ASP.NET 4.5 to market. Thanks to Scott Guthrie, Steve Smith, and Rob Howard for allowing me to join the ASPInsiders program and helping me get my content off the ground. Thanks to all of the ASPInsiders and Microsoft MVPs for their support over the years. Thanks to Frank Sorokach in allowing me to spend the time necessary to get my development career off the ground. Thanks to Louis Cesare and Sean Higgins for supporting my community initiatives and giving me time when I’ve needed to provide feedback to the ASP.NET team. Thanks to Luzerne County Community College and Penn State University for allowing our user group to prosper in Northeastern Pennsylvania. Finally, thanks to my family, specifically my parents, Deb and Tom Gaylord; my grandparents; my aunts and uncles; my wife, Lisa; and my children for their support. It’s not easy writing a book and they’ve certainly understood the time and dedication needed to get this book to ship.
—Jason N. Gaylord
EVERY BOOK IS A TEAM EFFORT with all the editors (acquisition, development, technical, etc.) involved, but with this title, the authors are quite a large team, too! So thank you to Jason, Pranav, Scott, and Todd, it has been an honor working with you. Also we are indebted to the authors of the previous editions, to everyone involved at Wiley, and to our tech editors.
About 7 years ago, I was working on another book for the Wrox imprint of Wiley, and promised Yvonne that I would invite her to a really sumptuous dinner at one of Europe’s top restaurants after it has been published. Things did not turn out as planned, including co-authors dropping out, so the book (and the dinner) never materialized. So let’s try again—would June 13 work?
—Christian Wenz
I WOULD LIKE TO THANK all the coffee shops owners in Seattle. The relaxing environment and a good coffee was my inspiration in writing the book.
—Pranav Rastogi
THANKS to Kelly for putting up with more late nights than normal and to my two princesses, Amber and Sydney, for putting up with less time and attention! I love you all. Thanks to Kevin, Mary, and the rest of the team at Wiley for their help and support on this book project. Also a big thank you to my co-authors. It has been great working with you on this book.
—Todd Miranda
BIG THANKS to Pranav Rastogi, Scott Hunter, Damian Edwards, Eilon Lipton, and the whole ASP.NET team that does the real work.
—Scott Hanselman
ASP.NET has continued to evolve over the years as the web has changed. When it was first released, Web Forms was a revolutionary model for bringing object-oriented programming to the web by using rich controls to encapsulate web behavior. Next when Ajax was starting to be a common term, ASP.NET evolved to contain a rich Ajax Library and an UpdatePanel control that simplified Ajax programming. Then as the Model-View-Controller pattern started becoming popular, we shipped ASP.NET MVC which provided a framework for building ASP.NET applications using the MVC pattern. And then ASP.NET went back to its original roots with ASP.NET Web Pages, which provided a simple PHP style of programming to help people learn to program for the web. As connected devices such as smartphones and tablets started appearing, programmers needed a way to build APIs that can be called from devices, and ASP.NET Web API was born. The latest addition is ASP.NET SignalR, which provides a programming model for applications that need to have real time events showing live updates of data, such as stock quotes.
Each time the web has changed, ASP.NET has changed with it, and we will continue to keep each ASP.NET framework in sync with the latest developments in the future. With ASP.NET 4.5, one of our goals was to make sure that Web Forms remained up to date with the advancements in the other frameworks listed above. Features like Model Binding, Unobtrusive JavaScript, and NuGet Libraries were added to Web Forms so Web Forms developers had access to the new features that were created in the newer frameworks. Tooling was added so ASP.NET Web APIs can be added to any ASP.NET project. You are witnessing the evolution of One ASP.NET which will enable all of our frameworks and features to work together.
Enjoy the beginning of One ASP.NET and ASP.NET.NET 4.5 with Professional ASP.NET 4.5 in C# and VB!
—Scott Hunter
Principal Program Manager, Web Platform Team, Microsoft