Discover millions of ebooks, audiobooks, and so much more with a free trial

Only $11.99/month after trial. Cancel anytime.

Professional ASP.NET 4.5 in C# and VB
Professional ASP.NET 4.5 in C# and VB
Professional ASP.NET 4.5 in C# and VB
Ebook3,104 pages22 hours

Professional ASP.NET 4.5 in C# and VB

Rating: 0 out of 5 stars

()

Read preview

About this ebook

The all-new approach for experienced ASP.NET professionals!

ASP.NET is Microsoft's technology for building dynamically generated web pages from database content. Originally introduced in 2002, ASP.NET has undergone many changes in multiple versions and iterations as developers have gained a decade of experience with this popular technology. With that decade of experience, this edition of the book presents a fresh, new overhauled approach.

  • A new focus on how to build ASP.NET sites and applications relying on field-tested reliable methods
  • Integration of "One ASP.NET" philosophy treating ASP.NET Web Forms and ASP.NET MVC as equal tools each with their proper time and place
  • Coverage of hot new ASP.NET 4.5 additions such as the Web API, Websockets and HTML5 & CSS3 use in layout but only to the extent that the tools themselves are practical and useful for working ASP.NET developers

Professional ASP.NET 4.5 in C# and VB is an essential tool for programmers who need to be productive and build reliably performing sites with the latest ASP.NET Framework and Visual Studio.

LanguageEnglish
PublisherWiley
Release dateApr 26, 2013
ISBN9781118332054
Professional ASP.NET 4.5 in C# and VB

Related to Professional ASP.NET 4.5 in C# and VB

Related ebooks

Internet & Web For You

View More

Related articles

Reviews for Professional ASP.NET 4.5 in C# and VB

Rating: 0 out of 5 stars
0 ratings

0 ratings0 reviews

What did you think?

Tap to rate

Review must be at least 10 words

    Book preview

    Professional ASP.NET 4.5 in C# and VB - Jason N. Gaylord

    INTRODUCTION

    SIMPLY PUT, ASP.NET 4.5 is an amazing technology to use to build your web applications! Since the inception of ASP.NET, numerous technologies have been introduced that make it one of the most ambitious web frameworks available today. ASP.NET 4.5 continues to build on the foundation laid by the previous releases of ASP.NET by focusing on the area of developer productivity.

    This book covers nearly all of what ASP.NET has to offer. It not only introduces new topics, but it also shows you examples of these new technologies in action. So sit back, pull up that keyboard, and enjoy!

    THE PAST, PRESENT, AND FUTURE OF ASP.NET

    ASP.NET 4.5 is another major release of the product and builds on the previous releases with additional capabilities. This release continues on a path to make ASP.NET developers the most productive developers in the web space. With each release of ASP.NET, the Microsoft team has focused its goals on developer productivity, administration, and management, as well as performance and scalability. This book focuses on the new additions to ASP.NET and the .NET Framework.

    ASP.NET has been around for over 10 years. When it was first released, the focus was to convert the existing web programming model of using VBScript or Jscript to build Active Server Pages to an object-oriented model. To assist in the transition, several server controls were included to encapsulate common functionality.

    As time passed, developers began to require more control over the rendered markup. As a result, ASP.NET changed. Newer web paradigms emerged, and technologies, such as ASP.NET MVC, were born. On the other hand, ASP.NET Web Forms continued to control the majority of the development. Thus, the past few releases of ASP.NET placed an emphasis on proper markup rendering with server controls.

    However, as the Visual Studio team, like most Microsoft teams, was on a 2- to 3-year release cycle, the ASP.NET team could not keep up with the changes in web technologies. Chapter 1 of this book provides a more comprehensive explanation as to how the ASP.NET team is handling this dilemma by providing more frequent out-of-band (OOB) releases of their products. This chapter also discusses the open source initiatives that the ASP.NET team has. In fact, many of the ASP.NET technologies have been already released as true open source.

    It’s a really exciting time for web development on ASP.NET.

    WHAT YOU NEED FOR ASP.NET 4.5

    You will most likely find that installing Visual Studio 2012 is best to work through the examples in this book. However, you can use Microsoft’s Notepad or WebMatrix and the command-line compilers that come with the .NET Framework 4.5. To work through every example in this book, you need the following:

    Windows 8, Windows 7, Windows Vista, Windows Server 2012, or Windows Server 2008

    Visual Studio 2012 (this installs the .NET Framework 4.5)

    SQL Server 2012, 2008, 2005, or SQL Server Express Edition

    The nice thing is that you are not required to have Microsoft Internet Information Services (IIS) to work with ASP.NET 4.5, because Visual Studio 2012 includes a built-in version of the web server called IIS Express. Moreover, if you do not have a full-blown version of SQL Server, don’t be alarmed. Many examples that use this database can be altered to work with Microsoft’s SQL Server Express Edition, which you can find free on the Internet.

    WHO THIS BOOK IS FOR

    This book was written to introduce you to the features and capabilities that ASP.NET 4.5 offers, as well as to give you an explanation of the foundation that ASP.NET provides. We assume you have a general understanding of web technologies, such as previous versions of ASP.NET or other web technologies such as PHP. If you understand the basics of web programming, you should not have much trouble following along with this book’s content.

    If you are brand new to ASP.NET, be sure to check out Wrox’s Beginning ASP.NET 4.5: In C# and VB by Imar Spaanjaars (John Wiley & Sons, 2012) to help you understand the basics.

    In addition to working with web technologies, we also assume that you understand basic programming constructs, such as variables, For Each loops, and object-oriented programming.

    You may also be wondering whether this book is for the Visual Basic developer or the C# developer. We are happy to say that it is for both! When the code differs substantially, this book provides examples in both VB and C#. In these cases, any HTML markup is omitted from the C# example for brevity.

    WHAT THIS BOOK COVERS

    This book explores the 4.5 release of ASP.NET. It covers each major new feature included in ASP.NET 4.5 in detail. The following list tells you something about the content of each chapter.

    Chapter 1, One ASP.NET — Extending a Healthy Web Ecosystem: The first chapter may just be one of the most interesting chapters in the book. After 10 years of existence, ASP.NET is one of the most prolific web development technologies. In this chapter, the future plans for ASP.NET are detailed, including a new vision: to have one ASP.NET.

    Chapter 2, HTML5 and CSS3 Design with ASP.NET: Visual Studio 2012 places an emphasis on building web applications using HTML5 and CSS3. This chapter takes a close look at how you can effectively work with HTML and CSS to design your ASP.NET applications.

    Chapter 3, ASP.NET Web Forms Structure: The third chapter covers the frameworks of ASP.NET applications as well as the structure and frameworks provided for single ASP.NET pages. This chapter shows you how to build ASP.NET applications with Visual Studio 2012. It also shows you the folders and files that are part of ASP.NET. It discusses ways to compile code and shows you how to perform cross-page posting. The chapter ends by showing you easy ways to deal with your classes from within Visual Studio 2012.

    Chapters 4 through 7, Controls: These four chapters are grouped together because they all deal with server or user controls. This batch of chapters starts by examining the idea of the server control and its pivotal role in ASP.NET development. In addition to looking at the server control framework, these chapters delve into the plethora of server controls that are at your disposal for ASP.NET development projects. Chapter 4, ASP.NET Server Controls and Client-Side Scripts, looks at the basics of working with server controls. Chapter 5, ASP.NET Web Server Controls, covers the controls that are part of the latest ASP.NET release. Chapter 6, Validation Server Controls, describes a special group of server controls: those for validation. You can use these controls to create beginning-to-advanced form validations. Chapter 7, User and Server Controls, describes building your own server controls and using them within your applications.

    Chapters 8 through 13, Data Access: This part of the book discusses data access. Nearly all web applications that exist today interact with at least one type of data source. From relational data to JSON, these chapters cover it all. Chapter 8, Data Binding, looks at the underlying capabilities that enable you to work with the data programmatically before issuing the data to a control. Chapter 9, Model Binding, covers the basics of binding and applying model binding to Web Forms. Chapter 10, Querying with LINQ, introduces you to LINQ and how to effectively use this feature in your web applications today. Chapter 11, Entity Framework, discusses mapping objects from the database to the objects within your code. Using Visual Studio 2012, you are able to visually design your entity data models. Chapter 12, ASP.NET Dynamic Data, describes how dynamic data enables you to quickly and easily put together a reporting and data entry application from your database. Chapter 13, Working with Services, outlines the differences between traditional ASMX Web Services, WCF, and the new WebAPI.

    Chapter 14, Introduction to the Provider Model: A number of systems are built into ASP.NET that make the lives of developers so much easier and more productive than ever before. These systems are built on an architecture called a provider model, which is rather extensible. This chapter gives an overview of this provider model and how it is used throughout ASP.NET 4.5.

    Chapter 15, Extending the Provider Model: After an introduction of the provider model, this chapter looks at some of the ways to extend the provider model found in ASP.NET 4.5. This chapter also reviews a couple of sample extensions to the provider model.

    Chapter 16, Working with Master Pages: Master pages are a great capability of ASP.NET. They provide a means of creating templated pages that enable you to work with the entire application, as opposed to single pages. This chapter examines the creation of these templates and how to apply them to your content pages throughout an ASP.NET application.

    Chapter 17, Site Navigation: It is quite apparent that many developers do not simply develop single pages — they build applications. Therefore, they need mechanics that deal with functionality throughout the entire application, not just the pages. One of the application capabilities provided by ASP.NET 4.5 is the site navigation system covered in this chapter. The underlying navigation system enables you to define your application’s navigation structure through an XML file, and it introduces a whole series of navigation server controls that work with the data from these XML files.

    Chapter 18, Personalization: Developers are always looking for ways to store information pertinent to the end user. After it is stored, this personalization data has to be persisted for future visits or for grabbing other pages within the same application. The ASP.NET team developed a way to store this information — the ASP.NET personalization system. The great thing about this system is that you configure the entire behavior of the system from the web.config file.

    Chapter 19, Membership and Role Management: This chapter covers the membership and role management system developed to simplify adding authentication and authorization to your ASP.NET applications. These two systems are extensive; they make some of the more complicated authentication and authorization implementations of the past a distant memory. This chapter focuses on using the web.config file for controlling how these systems are applied, as well as on the server controls that work with the underlying systems.

    Chapter 20, Security: This chapter discusses security beyond the membership and role management features provided by ASP.NET 4.5. This chapter provides an in-depth look at the authentication and authorization mechanics inherent in the ASP.NET technology, as well as HTTP access types and impersonations.

    Chapter 21, State Management: Because ASP.NET is a request-response–based technology, state management and the performance of requests and responses take on significant importance. This chapter introduces these two separate but important areas of ASP.NET development.

    Chapter 22, Caching: Because of the request-response nature of ASP.NET, caching (storing previously generated results, images, and pages) on the server becomes important to the performance of your ASP.NET applications. This chapter looks at some of the advanced caching capabilities provided by ASP.NET, including the SQL cache invalidation feature that is part of ASP.NET 4.5. This chapter also takes a look at object caching and object caching extensibility.

    Chapters 23 through 27, Client-Side Development: These five chapters touch upon one of the hottest areas of technology: mobile technologies. The day has come where consumers and businesses have the expectation of having real-time data delivery and applications with fluent designs that work from a cell phone, to a tablet, to a full-blown PC. Chapter 23, ASP.NET AJAX, takes a look at building your applications using AJAX. Chapter 24, Ajax Control Toolkit, reviews a series of controls available to make use of the AJAX technology. Chapter 25, jQuery, provides an overview of the jQuery JavaScript library. Chapter 26, Real-Time Communication, compares the methods for providing real-time communication traditionally with more modern methods such as using HTML5 Web Sockets or SignalR, which contains a hybrid solution. Chapter 27, Developing Websites with Mobile in Mind, specifically discusses methods for updating the client side to provide better mobile client support.

    Chapters 28 through 33, Application Configuration and Deployment: By this point, you should have a solid understanding of building an ASP.NET application. These six chapters focus on configuring and optimizing your application, and, finally, deploying the application. Chapter 28, Configuration, teaches you to modify the capabilities and behaviors of ASP.NET using the various configuration files at your disposal. Chapter 29, Debugging and Error Handling, tells you how to properly structure error handling within your applications and shows you how to use various debugging techniques to find errors that your applications might contain. Chapter 30, Modules and Handlers, looks at two methods of manipulating the way ASP.NET processes HTTP requests: HttpModule and HttpHandler. Each method provides a unique level of access to the underlying processing of ASP.NET. Chapter 31, Asynchronous Communication, discusses methods for performing one-way communication from the client to a method on the server or to a hosted service. Chapter 32, Building Global Applications, looks at some of the important items to consider when building your web applications for the world. Chapter 33, Packaging and Deploying ASP.NET Applications, takes the building process one step further and shows you how to package your ASP.NET applications for easy deployment.

    Chapter 34, ASP.NET MVC: ASP.NET MVC has generated a lot of excitement from the development community. ASP.NET MVC supplies you with the means to create ASP.NET applications using the Model-View-Controller models that many developers expect. ASP.NET MVC provides developers with the testability, flexibility, and maintainability in the applications they build. It is important to remember that ASP.NET MVC is not meant to be a replacement for the ASP.NET everyone knows and loves, but instead is simply a different way to construct your applications.

    Chapter 35, ASP.NET Web Pages and Razor: ASP.NET Web Pages is a newer technology that enables developers to use Razor syntax to build an interactive web application. This chapter provides an overview of building a Web Pages application using Microsoft WebMatrix. It also provides a listing of some of the more popular helpers that use the Razor syntax to render valid HTML and CSS.

    Appendix A, Migrating Older ASP.NET Projects: In some cases, you build your ASP.NET 4.5 applications from scratch, starting everything new. In many instances, however, this is not an option. You need to take an existing ASP.NET application that was built on a previous version of the .NET Framework and migrate the application so that it can run on the .NET Framework 4.5.

    Appendix B, COM Integration: Invariably, you will have components created with previous technologies that you do not want to rebuild, but that you do want to integrate into new ASP.NET applications. If this is the case, the .NET Framework makes incorporating your previous COM components into your applications fairly simple and straightforward. This appendix also shows you how to build .NET components instead of turning to the previous COM component architecture.

    Appendix C, ASP.NET Ultimate Tools: This appendix takes a look at the tools available to you as an ASP.NET developer. Many of the tools here will help you to expedite your development process and, in many cases, make you a better developer.

    Appendix D, Administration and Management: Besides making it easier for the developer to be more productive in building ASP.NET applications, the ASP.NET team also put considerable effort into making the managing of applications easier. In the past, using ASP.NET 1.0/1.1, you managed ASP.NET applications by changing values in an XML configuration file. This appendix provides an overview of the GUI tools that come with ASP.NET today that enable you to manage your web applications easily and effectively.

    Appendix E, Dynamic Types and Languages: As of the release of ASP.NET 4.5, you can now build your web applications using IronRuby and IronPython. This appendix takes a quick look at using dynamic languages in building your web applications.

    Appendix F, ASP.NET Online Resources: This small appendix points you to some of the more valuable online resources for enhancing your understanding of ASP.NET.

    Appendix G, Visual Studio Extensibility with NuGet: Visual Studio 2012 provides an extensibility model by using Microsoft’s version of NuGet called Package Manager. NuGet enables developers to share files, folders, and binaries by creating a distributable package. The packages can be made available on the public feed or within a private feed.

    CONVENTIONS

    This book uses a number of different styles of text and layout to help differentiate among various types of information. Here are examples of the styles used and an explanation of what they mean:

    New words being defined are shown in italics.

    Keys that you press on the keyboard, such as Ctrl and Enter, are shown in initial caps and spelled as they appear on the keyboard.

    File extensions, URLs, and code that appears in regular paragraph text are shown in a monospaced typeface.

    A block of code that you can type as a program and run is shown on separate lines, like this:

        public static void Main()

      {

          AFunc(1,2,abc);

      }

    or like this:

        public static void Main()    {      AFunc(1,2,abc);    }

    Sometimes you see code in a mixture of styles, like this:

              // If we haven't reached the end, return true, otherwise

              // set the position to invalid, and return false.

              pos++;

             

             

    if (pos < 4)

                return true;

     

              else {

                pos = -1;

                return false;

              }

    When mixed code is shown like this, the bold code is what you should focus on in the current example.

    We demonstrate the syntactical usage of methods, properties, and so on using the following format:

      SqlDependency="database:table"

    Here, the italicized parts indicate placeholder text: object references, variables, or parameter values that you need to insert.

    Most of the code examples throughout the book are presented as numbered listings that have descriptive titles, like this:

    LISTING I-3: Targeting mobile devices in your ASP.NET pages

    Each listing is numbered (for example, Listing 1-3) where the first number represents the chapter number and the number following the hyphen represents a sequential number that indicates where that listing falls within the chapter. Downloadable code from the Wrox website (www.wrox.com) also uses this numbering system (in many cases) so that you can easily locate the examples you are looking for. In other cases where listing numbers are not used, the authors have included filenames in the chapter that point you to where the code is in the download.

    All code is shown in both VB and C#, when warranted. The exception is for code in which the only difference is, for example, the value given to the Language attribute in the Page directive. In such situations, we don’t repeat the code for the C# version; the code is shown only once, as in the following example:

      <%@ Page Language=VB%>

      http://www.w3.org/1999/xhtml>

      server>

          DataSetDataSource

     

     

         

    form1 runat=server>

            Dropdownlist1 Runat=server DataTextField=name   

              DataSourceID=XmlDataSource1>

           

            XmlDataSource1 Runat=server

              DataFile="

    /Painters.xml">

           

       

     

     

    NOTE Throughout the book, we may reference the default folder for the .NET Framework at C:\WINDOWS\Microsoft.NET\Framework\v4.0.xxxxx\. However, if you are using a 64-bit version of Windows and have the x64 version of the .NET Framework installed, you can find the 64-bit version of the framework at C:\WINDOWS\Microsoft.NET\Framework64\v4.0.xxxxx\.

    SOURCE CODE

    As you work through the examples in this book, you may choose either to type all the code manually or to use the source code files that accompany the book. As previously indicated, all the source code used in this book is available for download at www.wrox.com. When you get to the site, simply locate the book’s title (either by using the Search box or one of the topic lists) and click the Download Code link. You can then choose to download all the code from the book in one large Zip file or download just the code you need for a particular chapter.

    NOTE Because many books have similar titles, you may find it easiest to search by ISBN; this book’s ISBN is 978-1-118-31182-0.

    After you download the code, just decompress it with your favorite compression tool. Alternatively, you can go to the main Wrox code download page at www.wrox.com/dynamic/books/download.aspx to see the code available for this book and all other Wrox books. Remember that you can easily find the code you are looking for by referencing the listing number of the code example from the book, such as Listing 1-2. We used these listing numbers when naming most of the downloadable code files. Those listings that are not named by their listing number are accompanied by the filename so you can easily find them in the downloadable code files. Also, you can download the versions of the AdventureWorks and Northwind databases we used for this book from www.wrox.com/go/SQLServer2012DataSets.

    ERRATA

    We make every effort to ensure that there are no errors in the text or in the code. However, no one is perfect, and mistakes do occur. If you find an error in one of our books, such as a spelling mistake or faulty piece of code, we would be very grateful if you would tell us about it. By sending in errata, you may spare another reader hours of frustration; at the same time, you are helping us provide even higher-quality information.

    To find the errata page for this book, go to www.wrox.com and locate the title using the Search box or one of the title lists. Then, on the book details page, click the Book Errata link. On this page, you can view all errata that have been submitted for this book and posted by Wrox editors. A complete book list including links to each book’s errata is also available at www.wrox.com/misc-pages/booklist.shtml.

    If you do not spot your error already on the Book Errata page, go to www.wrox.com/contact/techsupport.shtml and complete the form there to send us the error you have found. We will check the information and, if appropriate, post a message to the book’s errata page and fix the problem in subsequent editions of the book.

    P2P.WROX.COM

    For author and peer discussion, join the P2P forums at p2p.wrox.com. The forums are a web-based system for you to post messages relating to Wrox books and technologies and to interact with other readers and technology users. The forums offer a subscription feature that enables you to receive e-mail on topics of interest when new posts are made to the forums. Wrox authors, editors, other industry experts, and your fellow readers are represented in these forums.

    At http://p2p.wrox.com you will find a number of different forums that will help you not only as you read this book, but also as you develop your own applications. To join the forums, just follow these steps:

    Go to p2p.wrox.com and click the Register link.

    Read the terms of use and click Agree.

    Supply the information required to join, as well as any optional information you want to provide, and click Submit.

    You will receive an e-mail with information describing how to verify your account and complete the joining process.

    NOTE You can read messages in the forums without joining P2P, but you must join in order to post messages.

    After you join, you can post new messages and respond to other users’ posts. You can read messages at any time on the web. If you want to have new messages from a particular forum e-mailed to you, click the Subscribe to this Forum icon by the forum name in the forum listing.

    For more information about how the forum software works, as well as answers to many common questions specific to P2P and Wrox books, be sure to read the P2P FAQs. Simply click the FAQ link on any P2P page.

    PART I

    ASP.NET Fundamentals

    CHAPTER 1: One ASP.NET

    CHAPTER 2: HTML5 and CSS3 Design with ASP.NET

    CHAPTER 3: ASP.NET Web Forms Structure

    1

    One ASP.NET

    WHAT’S IN THIS CHAPTER?

    Introducing One ASP.NET

    Exploring and simplifying the complex web ecosystem

    Benefiting from One ASP.NET

    ASP.NET has been a prominent web development framework that has supported .NET developers in building web applications for years. As the Internet has matured and progressed, so have the ASP.NET framework and the ecosystem around it. Thousands of products, services, and open source projects call ASP.NET home.

    The Internet has advanced much faster than ASP.NET. The advancements in HTML, CSS, and JavaScript have led to richer experiences for users, which put pressure on ASP.NET to support these emerging technologies. The ASP.NET framework has had to support the growing needs of web developers. Although all this has led to the development of a healthy ecosystem where web developers from all over have come together to make it better, it’s also caused core pieces of ASP.NET to show its age.

    This chapter looks at how ASP.NET as a framework has matured, and proposes some ways to fix the problems with the ecosystem. How can ASP.NET be a trusted and reliable framework that the enterprise developer can count on, while still meeting the needs of the advanced web developer who demands the latest standards? The goal of the chapter is to give you an idea of some of the changes that have happened in ASP.NET and where the framework is headed.

    INTRODUCING ONE ASP.NET

    A few years back when ASP.NET MVC was just coming out, a number of people told me they were being discouraged from making hybrid applications. They wanted ASP.NET apps with services components, MVC areas, and Web Forms pieces. Some Microsoft employees made them feel that this was an unsupported thing. Why would you want to do that? That’s not a good idea.

    But it is. ASP.NET MVC is ASP.NET. Web Forms is ASP.NET. The pipeline set in place more than 10 years ago is with us today, offering many extensibility points that have been exploited not only by ASP.NET but also by alternative frameworks like NancyFx and ServiceStack.

    Why make developers choose from a half dozen different flavors when it’s all the same underlying menu? ASP.NET offers core services that you need regardless of your application architecture. Key management, session management, caching, HTTP, authorization, and authentication are all universal web development truths. How you choose to render your angle brackets or curly braces is your preference, but you should feel comfortable using whatever ASP.NET framework or frameworks help you solve your business problem. You should be able to do this not only without guilt, but also in a totally supported and encouraged way. Figure 1-1 shows a breakdown of the different frameworks in ASP.NET and how they all use a common underlying ASP.NET core.

    FIGURE 1-1

    One ASP.NET is all about making it easier for developers to build applications using ASP.NET and Visual Studio. One ASP.NET is a general term and can mean a lot of things. Let’s break it down.

    In today’s world, you have a lot of choices and options when you think about building web applications. Each one has its own merits and drawbacks. When you set out to build applications, how do you know which framework to choose?

    If you choose one and then realize it was not a good choice, how easily can you come back and go another route? Was your framework modular enough for you to swap out the offending piece, or did you have to rewrite everything? Are you able to share code between subsystems?

    How often should your framework update itself? The Internet moves far faster than most any framework can handle. How can ASP.NET serve two masters? You want HTML5 and CSS3 now, but you can’t break an app your business relies on.

    One ASP.NET should give developers the confidence that, no matter what they choose, they are still developing on a trusted underlying framework — ASP.NET. To help you understand One ASP.NET, the following section explores how we got here.

    Evolution of ASP.NET

    Web development for Microsoft started with Classic ASP (Active Server Pages) in the late 90s. A developer was able to write dynamic pages based on the VB scripting language.

    ASP.NET 1.0 was introduced in 2002 and was built on top of the .NET Framework. ASP.NET Web Forms simplified developers’ transition from Windows application development to web development by enabling them to build pages composed of controls, similar to a Windows user interface. It was unlike anything we’d ever seen! Visual Basic developers who were used to dragging data grids onto Windows Forms could drag data grids onto Web Forms! It was magic.

    It was magic because it layered state on top of stateless HTTP. It allowed developers to react to business-level events like Button.Click rather than low-level events like HTTP POST, enabling a whole new generation of developers to create great web applications.

    ASP.NET 2.0 was released in 2005 along with Visual Studio 2005. This release took the controls metaphor even further with data-focused enhancements like the SqlDataSource and ObjectDataSource. ASP.NET 3.5 followed in 2008 and brought with it DynamicData, which enables you to rapidly generate data-driven applications in minutes.

    Shortly thereafter, ASP.NET MVC was released and introduced the popular Model-View-Controller pattern to ASP.NET web development for the first time. Still based on ASP.NET, ASP.NET MVC provided a level of testability and composition as yet unseen on the .NET platform. ASP.NET MVC was developed to meet the growing needs of a developer community that wanted to easily unit-test their applications and have more granular control over how HTML markup was generated. ASP.NET MVC represented a level of absolute control and power that brought a new kind of developer to the .NET Framework, and perhaps also allowed some older, disenfranchised developers to stay on ASP.NET.

    A few things were interesting about MVC. ASP.NET MVC was one of the first releases for the ASP.NET framework where a part of the framework was released by itself out of band (OOB). It meant that ASP.NET MVC was released as a standalone installer that developers could optionally download and add onto their existing Visual Studio installation. This was a huge step toward making it possible for developers to get new products quickly rather than waiting for 2 to 3 years. The ASP.NET team didn’t realize it was happening, but ASP.NET MVC as an out-of-band release arguably marked the beginning of the ASP.NET team’s break from the Visual Studio ship train.

    Over the next 2 years, two more versions of ASP.MVC were released. More significantly, ASP.NET MVC was released under the Microsoft Public License (MS-PL), which allows developers to see the source of their favorite framework. Whispers began that perhaps ASP.NET MVC might be released as proper open source, but Microsoft would never do that, right?

    ASP.NET 4 was released along with VS2010. At the same time ASP.NET MVC 4 was released, ASP.NET Web Pages was introduced with a fantastic new syntax for dynamic page creation called Razor. ASP.NET Web Pages and the new Razor syntax provide a fast, approachable, and lightweight way to combine server code with HTML to create dynamic web content. Razor became the new view engine behind ASP.NET MVC. ASP.NET Web Pages brought Razor to a simple new programming model suitable for smaller sites or folks just getting started.

    ASP.NET 4.5 was released along with VS2012 in 2012. This release included ASP.NET MVC 4.5 and a new member into the ASP.NET family called ASP.NET Web API, which made it easier to write REST-based web services.

    Shortly thereafter, Microsoft released the majority of its Web Stack (including ASP.NET MVC, Razor, and ASP.NET Web API) under an open source license (Apache License 2.0). But it also announced it would be taking contributions from the community — real open source on a strategically important and flagship web framework. The big ship was starting to turn.

    This was the beginning of development in the open. An interested community member can actually read the code checkins while the developers are working inside Microsoft. Even better, contributors can fix bugs, add features, and submit pull requests knowing their code could be used by millions of developers.

    Open development was initially scary, but doing so enabled a more open development model where everyone is engaged and can provide feedback on code checkins, bug-fixes, new feature development, and build and test the products on a daily basis using the most up-to-date versions of the source code and tests. This was a gigantic step in getting the ASP.NET ecosystem to come together in a way that developers could work together toward building a better framework.

    At this point in the story, we have a large part of ASP.NET and its runtime components shipping out of band as open source. Many features are optional, or easily added on, like Web Optimization, Universal Membership Providers, and test harnesses. How can the tooling be opened up to support a simultaneously more uniform but modular ASP.NET?

    The Web Evolves and We Evolve with It

    HTML5 isn’t done, and unless this chapter is published in 2017, it still won’t be done at the time of this writing. However, HTML5, along with its siblings CSS3 and JavaScript, have won the Internet. Now that a reliable and complete version of JavaScript is available on every modern browser, combined with a cohesive document object model (DOM), applications have changed their architecture dramatically.

    JavaScript has moved beyond simple alert() statements and input type validation and graduated to being a near-complete virtual machine in the browser. HTML5 is very simple as a spec, and CSS3 makes it shine. Take all these ingredients and combine them with a pocket super computer, and you’ve got a mobile web revolution.

    Today, customers are using different form factors, ranging from smartphones to tablets to personal computers. This means that they can access a web application using a phone or tablet, or using a mobile browser, or using the traditional desktop browser. The web is going more social as well. Users are interacting with each other through social means such as Twitter, Facebook, and so on, and they want to carry their social identities in all web applications with which they interact. This means that they want to log in using their social credentials, so that when they log in to the application they can interact with their friends as well. It’s a world of connected devices and services for the web developer.

    As the web becomes more ubiquitous and web standards continue to evolve, it is crucial that the ASP.NET framework rev at a frequency that matches the advancements in web development.

    SIMPLIFYING A COMPLEX ECOSYSTEM

    File New Project is too scary. It forces an artificial choice and makes developers feel that they’ve proceeded down a fork in the road that may not be reversible. Perhaps, rather than a fork in the road forcing an unnatural choice, we instead consider a unified face to ASP.NET with some choice to select possible subsystems, libraries, or alternative application frameworks.

    So you make your choice. How do you now discover and integrate libraries to easily write services in your application? What if you take the safe choice and choose Empty Web Application? Does that make it easier to add services? If you choose ASP.NET Web Application, how do you add DynamicData functionality to this application?

    What about social? You can bring in a social support library, but can you easily integrate it into both MVC and Web Forms? From a developer’s perspective, this is a fairly complicated story that leaves you hanging, trying to figure out not just the best way to get started, but also how to extend your application. Figure 1-2 shows the choices you have when creating a new project.

    FIGURE 1-2

    Web Ecosystem

    Web development is an exciting and complicated space to be in. A lot of variables impact the productivity of a web developer. As a web developer, you have to constantly be versed in the latest and greatest in terms of web standards, and once you know what the latest standards are, you then have to worry whether the framework and tools you are using support those standards. Once you know that, how easy is it for you to get the FX, tools, and libraries that you want to use in your application?

    A big part of this developer experience is the growing ecosystem. As the web expands and grows, you can find an explosion of libraries that help you develop your applications. These libraries can become popular and widely accepted by the developer community based on the problems they solve. For example, more general libraries such as jQuery are widely popular, and jQuery is the library of choice for many developers who use JavaScript. In addition, lots of specialized libraries are solving unique problems that might not apply to the majority of developers.

    We are at a point where lots of such libraries are available, such as Entity Framework, JSON.NET, ELMAH, and so on that are open sourced and can be used in your applications. A big part of your application now comprises a mixture of these open sourced libraries and the ones that come from Microsoft. So the question is that in this growing ecosystem, how easy is it to find such libraries and easily bring them into your applications? Also, once you have these libraries, how do you ensure that they are always updated to the latest versions? Figure 1-3 shows the NuGet gallery with the most popular packages. These packages have been downloaded more than a million times each, and at the time of writing there have been total of 50 million downloads of packages from the NuGet gallery.

    FIGURE 1-3

    Web development has definitely evolved over the years, and it is an exciting time to be a web developer. Web developers no longer have to spend time adding basic building blocks to their applications. Features such as logging are so common that developers can reuse a plethora of logging libraries in their applications. How easy is it for you to start with something really small and add these so-called Lego pieces to your application to get the job done more quickly? And how can you now focus on writing code that is central to your applications and bring in these different building blocks as and when required?

    The next few sections look at how ASP.NET framework and Visual Studio tooling can help you solve these problems and improve your productivity.

    Getting Started Is Easy

    In Visual Studio 2012 when you choose File New Project, you get lots of choices on what type of project you want to create. You can choose from creating a Single Page Application (SPA) or have an application that uses Social Login, or have an application that uses Windows Authentication. This becomes a problem when you want to create an application where you want to mix these scenarios. There is no easy way to get started built into Visual Studio. The templates represent a static list of options, but things would be simpler with a single One ASP.NET template, as shown in Figure 1-4.

    FIGURE 1-4

    NOTE This is just a conceptual idea at the time of writing this book.

    Once you create an ASP.NET project, Visual Studio can guide you through a series of steps that help you get started with an SPA framework of your choice, and then enable you to choose how you want users to log in to your application.

    The next section looks at how you can get more Lego pieces, or update the ones you have, once you start with such an application.

    Integrating the Ecosystem Is Easy

    In the previous section you looked at how easy it would be to create an SPA application with all the required libraries, such as Knockout, jQuery, ASP.NET Web API, and so on. In Visual Studio 2012, all the libraries are installed as NuGet packages. You can see what libraries are installed in your project by right-clicking the project and clicking Manage NuGet Packages. Figure 1-5 shows this option.

    FIGURE 1-5

    This launches the Manage NuGet Packages window, where you can choose the Installed packages tab and see all the packages that are installed in your project. Figure 1-6 shows all the packages installed in a project.

    FIGURE 1-6

    This window shows you the list of packages that were installed when you created the application. This application has packages that are a mix of Microsoft owned and open sourced libraries as well. This section discusses how NuGet helps you to integrate the ecosystem easily into your project.

    NuGet is a Visual Studio extension that makes it easy to add, remove, and update libraries and tools in Visual Studio projects. NuGet works on both Visual Studio 2010 and Visual Studio 2012, and you can download it from the Visual Studio Extension Gallery. If you develop a library that you want to share with other developers, you can create a NuGet package and upload the package to the NuGet gallery. If you want to use a library or tool that someone else has developed, you can download the package from the gallery and install it in your Visual Studio project. Simply put, NuGet is changing the .NET developer ecosystem by making it easy to redistribute and install packages into a project.

    Imagine that you had to install ELMAH into your application. Without NuGet, you would have to download an installer and then manually add references to the ELMAH library from the install location. Beyond this, you would have to read the documentation on the ELMAH website to make all the necessary changes to the web.config to configure ELMAH. If a new version of ELMAH came out during the course of your development, you would have no way of knowing that the version you have in your project is outdated and that you need to download a new version.

    NuGet makes this entire process of finding, installing, configuring, and updating libraries very easy. It is a one-stop solution that can solve all of these problems.

    Figure 1-6 showed the packages that were installed in an SPA application. Imagine that you were working on this project for a while and wanted to check for updates to any of the libraries that you were using. To do this, you would simply launch the Manage NuGet Packages window and click the Updates tab. Figure 1-7 shows the updates available for the packages.

    FIGURE 1-7

    You click Update on the package that you want to update, and NuGet downloads a new version of that package and updates the references as well. You can easily update any of the packages quickly.

    NOTE When you create any new project in Visual Studio 2012, the project already has NuGet packages installed so you can get these benefits easily.

    Real-World Example

    This section shows how you can apply One ASP.NET in a practical real-world application. The following approach by no means represents any best practice, but it is meant to demonstrate how One ASP.NET and the web ecosystem can come together to help you build an ASP.NET application. Assume that you wanted to create a simple single page to-do list web application with the following requirements:

    A user should be able to log in using Facebook, Twitter, or register as an account on the website.

    A user should be able to create to dos and mark items when done.

    An administrator should be able to manage user accounts via an administrative section.

    You can get started by choosing the ASP.NET SPA template and configuring the template to use Facebook, Twitter, and Local login. Once you have this template ready, you can start building your application.

    First, implement an ASP.NET Web API so you can have a REST-based service to manage to-do items. You can add a Web API by right-clicking your project and adding a new item template — ASP.NET Web API Controller Class (see Figure 1-8).

    FIGURE 1-8

    When you install this item template, it installs all the NuGet packages needed for running a Web API in ASP.NET.

    Write some code to implement to-do functionality of your application that includes features such as adding, deleting, modifying, and getting all to dos. Figure 1-9 shows a snippet of code for retrieving a list of to-do items.

    FIGURE 1-9

    While writing this To do Web API, you will want to test out the functionality without running your entire application, because it can become quite cumbersome. You can use NuGet to search for some test clients for testing your To do Web API. A quick search might return a list of packages from which you can choose one based on its rating. For example, you could download the WebApiTestClient NuGet package, which was popular at the time of writing this chapter. Figure 1-10 shows the result when you use this test client to test your Web API.

    FIGURE 1-10

    Now that you have the Web API working, you can start adding SPA libraries so that you can write the front end of the application. Again, you can search on NuGet for an SPA library of your choice. For this example, choose Knockout. Once you install the NuGet package, it brings in all the required libraries for using Knockout.

    After you install Knockout, you can create web pages where you can call your To do Web API and bind the results in Knockout views. The Visual Studio editor provides rich support for Knockout syntax highlighting and IntelliSense, which makes it easier to use Knockout. Figure 1-11 shows the syntax highlighting support for Knockout in Visual Studio.

    FIGURE 1-11

    Once you have the client-side data binding working with Knockout, you will have a fully functional SPA application. If you want to receive updates about your to-do items, you can get real-time update functionality by installing ASP.NET SignalR. You can do this by downloading the Microsoft.AspNet.SignalR NuGet package. Once you install this package, you can modify your application code to add real-time functionality. Figure 1-12 shows the to-do application that you just developed.

    FIGURE 1-12

    The final part in creating your application is to build some user account management for administrators. You can quickly build this part using ASP.NET Web Forms and Dynamic Data, which is a good framework for rapidly building data-driven applications. You can also secure this section with ASP.NET authorization using roles so that only administrators can access this section of the website.

    Finally, if you look at this application, it is a mixture of ASP.NET frameworks and technologies such as ASP.NET Web Forms, MVC, Web API, and SignalR working in cohesion with open source libraries such as Knockout, jQuery, JSON.NET, and many more. With all these different sets of frameworks and libraries, you are still developing on a trusted underlying framework — ASP.NET.

    HOW DO YOU BENEFIT?

    Now that you have read through all of this, you must be thinking that this is all great, but what is in it for you as a developer? How do you benefit from One ASP.NET? This section looks at some of the scenarios where One ASP.NET will help increase your developer productivity. It also looks at the common problems faced by developers today, and how ASP.NET and Visual Studio can help solve these problems.

    ASP.NET Makes Getting Started Easy

    With ASP.NET and Visual Studio, you should be able to:

    Start with nothing (an empty project) and add the required pieces of the framework and libraries needed to build your application.

    Choose the set of frameworks and libraries you need to build your application.

    One ASP.NET will make the ASP.NET framework more modular so you can get started with an application and be able to easily add the required set of pieces needed to build your application.

    ASP.NET Supports the Web Ecosystem

    We all know by now that future web applications will be much richer in terms of the user experience that they will offer. Also, with the growth of the .NET developer community, there has been an explosion of libraries being created and distributed amongst developers. In the .NET ecosystem, developers are now relying more and more on open sourced libraries and are contributing to these libraries to make them even better.

    The majority of the Microsoft Web Stack is now open sourced. Products such as ASP.NET MVC, ASP.NET Web Pages, ASP.NET Web API, ASP.NET SignalR, and Entity Framework are open sourced. This means that developers who do not work for Microsoft can make contributions to these projects.

    NOTE Even though these products are open sourced, they continue to be fully supported and staffed by Microsoft.

    Apart from making contributions, you can browse the source code, subscribe to checkin notifications, and browse through the active list of issues that developers on these products are working on. By doing all of this, you, as a developer, have more direct impact to the features being designed and developed in these products, which eventually results in a better product being developed.

    ASP.NET Makes Finding, Adding, and Updating Lego Blocks to Your Site Easy

    Although the growing ecosystem is a great sign for the web developer community, today there is a problem of discovering and installing all these great libraries in this ecosystem. The installation experience for such libraries has centered around downloading an installer and running through the installation steps. This makes the process of adding, configuring, and updating libraries in the project very hard. In contrast, NuGet is a giant step forward in this space. Libraries/frameworks can be redistributed as NuGet packages, which can be easily installed into your project through Visual Studio.

    ASP.NET Helps You Apply Concepts from One Framework to Another

    The ASP.NET umbrella has lots of different features that were developed in one framework, such as ASP.NET MVC, and have now made their way into other frameworks, such as ASP.NET Web Forms, and vice versa. For example:

    There was a feature called Data Annotation Attributes that was introduced in ASP.NET Dynamic Data and later introduced in ASP.NET MVC.

    Routing and model binding were some of the highlights of ASP.NET MVC that eventually made their way into ASP.NET Web Forms.

    ASP.NET Web Pages had a simple routing model called Smarty Routes that let you generate cleaner looking URLs without any configuration. This feature made its way into ASP.NET Web Forms and is now called ASP.NET FriendlyURLs.

    This means that if you are an ASP.NET developer, you can use these well-known concepts or features and apply them to any framework of your choice. If you like model binding and are a Web Forms developer, now you can continue building Web Forms applications while using new and well-known features such as model binding. You do not have to switch to another framework such as ASP.NET MVC, just for the simple reason that ASP.NET MVC supports model binding.

    ASP.NET Moves as Fast as the Web

    The web is evolving at a crazy pace. This is caused by many factors. The proliferation of devices and services has caused new kinds of applications to emerge that need to be more closely interconnected. The web is going more social, where users want to collaborate with each other in real time. To support all these changes, the Web Standards Consortium is pushed to make sure the specifications of HTML, JavaScript, and CSS are updated quickly to support these demands. Given all these changes, developers are pushed as well to stay abreast of all of them and demand that the frameworks and tools they use are updated to these latest standards as well.

    ASP.NET and Visual Studio have been responding to these changes as well. When ASP.NET 2.0 was released, it was released with VS2005, which was released 3 years after ASP.NET 1.0. This meant that developers had to wait for 3 years to get the latest framework that supported the latest web standards. We are moving to a new world now where parts of ASP.NET can be released more frequently and updated easily without waiting for the next release of Visual Studio.

    ASP.NET MVC, Web Pages, Web API, and SignalR are released as NuGet packages on the NuGet gallery so you can update these frameworks by updating the NuGet packages installed in your project. Microsoft ASP.NET and Web Frameworks 2012.2 and Microsoft Web Developer Tools 2012.2 add support for a number of improvements to make it easy to write web applications in VS and publish them to Azure. Visual Studio 2012 shipped with 1.0 version of Microsoft Web Developer Tools, but by the time this book is published this would be 1.2. This means in a matter of a few months as a developer you were able to get the latest tools to increase your productivity while building web applications. The ASP.NET team is moving toward a more frequent release schedule to be able to deliver updated tools and libraries to make web development more relevant for the emerging standards and libraries.

    SUMMARY

    ASP.NET should be a reliable, fun, modular, powerful, and scalable platform for building web applications. Whether you’re using a reporting control in Web Forms from a third-party control builder, or writing your own custom HTML Helper, ASP.NET can be a platform you should feel confident to have underlying your work.

    Perhaps your solution is developed using TDD and Agile with SpecFlow and targeting ASP.NET MVC, or perhaps you prefer Ember.js and ASP.NET Web API. Likely you’ll mix and match, and as well you should. ASP.NET is certainly becoming a web framework with choices.

    Don’t be afraid to try new things, swap out components, or explore new open source libraries. ASP.NET and the team will try to solve the big, hard problems of scale, security, and performance. How you architect your application is a personal choice, and you should have no concerns about ASP.NET’s ability to rise to the occasion and support your architecture.

    With the release of ASP.NET and Web Tools 2012.2, ASP.NET is about halfway to the dream of One ASP.NET. Someday soon perhaps Microsoft project templates will live side by side with community templates, and project templates will be easily shared and distributed as NuGet packages. The most recent version of ASP.NET doesn’t make it too hard to imagine that future.

    2

    HTML5 and CSS3 Design with ASP.NET

    WHAT’S IN THIS CHAPTER?

    Understanding the basics of HTML5

    Understanding the basics of CSS3

    Using HTML5 and CSS3 in ASP.NET applications

    WROX.COM CODE DOWNLOADS FOR THIS CHAPTER

    Please note that all the code examples in this chapter are available as a part of this chapter’s code download on the book’s website at www.wrox.com on the Download Code tab.

    When HTML was first introduced by Tim Berners-Lee, it was intended to be a simple way for researchers using the Internet to format and cross-link their research documents. At the time, the web was still primarily text-based; therefore, the formatting requirements for these documents were fairly basic. HTML needed only a small handful of basic layout concepts, such as a page title, section headers, paragraphs, and lists. As the web was opened up to the general public, graphical browsers were introduced, and as requirements for formatting web pages continued to expand, newer versions of HTML were introduced. These newer versions expanded the original capabilities of HTML to accommodate the new, rich graphical browser environment, allowing table layouts, richer font styling, images, and frames. In 1997, HTML was standardized to HTML4.

    Although all of these improvements to HTML were helpful, HTML still proved to be inadequate for allowing developers to create complex, highly stylized web pages. In 1994, a new technology called Cascading Style Sheets (CSS) was introduced. CSS served as a complementary technology to HTML, giving developers of web pages the power they needed to control the style of their web pages.

    As the web has matured, CSS has gained popularity as developers realized that it has significant advantages over standard HTML styling capabilities. Unlike HTML, which was originally conceived as primarily a layout mechanism, CSS was conceived from the beginning to provide rich styling capabilities to web pages. The cascading nature of CSS makes it easy to apply styles with a broad stroke to an entire application and overrides those styles only where necessary. CSS allows externally defining website style information easy, allowing for a clear separation of web page style and structure. CSS also allows developers to greatly reduce the file size of a web page, which translates into faster page load times and reduced bandwidth consumption.

    Since the turn of the century, mobile devices and multimedia streaming have gained in popularity. As Web 2.0 applications continue to evolve, so has HTML and CSS. Over the past couple of years, HTML5 and CSS3 have become more prevalent. The chapter starts with a brief overview of HTML5 and CSS3, and finishes with creating websites in Visual Studio using HTML and CSS. It’s a good idea to review these technologies because the project templates found in Visual Studio 2012 have been reworked using HTML5 and CSS3.

    CAVEATS

    Although this chapter includes a lot of great information about HTML and CSS, and how you can use them in conjunction with ASP.NET and Visual Studio, you should be aware of several caveats.

    First, because there is no way that a single chapter can begin to cover the entire breadth of HTML and CSS, if you are looking for an in-depth discussion of these topics, you can check out the Wrox title HTML5 24-Hour Trainer, by Joseph W. Lowery and Mark Fletcher (Wiley, 2011).

    Second, because CSS is simply a recommended specification, the interpretation and implementation of that specification is up to each browser vendor. As is so often the case in web development, each browser has its own quirks in how it implements (or sometimes does not implement) different CSS features. Even though the samples in this chapter were tested on Internet Explorer 10, be sure to thoroughly test your websites in multiple browsers on multiple platforms to ensure that your CSS is rendering appropriately in each browser you are targeting.

    Finally, both HTML5 and CSS3 are works in progress. CSS3 is unlike previous versions in that it’s made up of multiple modules. As of this book’s publication date, four modules have been published as formal recommendations. Although the CSS3 recommendation has not been finalized, the first official draft of CSS4 has been published in September, 2011 by the World Wide Web Consortium (W3C). HTML5 is still in its draft phase. In July 2012, the HTML5 specification editor Ian Hickson, announced that the W3C plans to take a snapshot of the HTML5 specification in 2014. This snapshot will be known as HTML5. The Web Hypertext Application Technology Working Group (WHATWG)

    Enjoying the preview?
    Page 1 of 1