Building ASP.NET Web Pages with Microsoft Webmatrix
Total Page:16
File Type:pdf, Size:1020Kb
BOOKS FOR PROFESSIONALS BY PROFESSIONALS® Lydford RELATED Building ASP.NET Web Pages with Microsoft WebMatrix Building ASP.NET Web Pages with Microsoft WebMatrix shows you how to quickly and easily create dynamic, data-driven web sites. It gives you the knowledge and skills to create custom web applications using the revolutionary Microsoft WebMatrix plat- form. You’ll learn every part of the toolset in detail and see how you can use it to com- plete common web development tasks. Building ASP.NET Web Pages with Microsoft WebMatrix teaches you how to: • Use ASP.NET Web Pages and the powerful new Razor syntax to combine HTML, CSS and custom server-side C# code • Accept input from HTML forms and implement code to validate user input • Develop dynamic pages that interact with databases to perform create, read, update, and delete operations • Make use of the built-in membership systems to manage site membership, authentication and security • Use the WebMatrix Packages Administration tool to find and install third-party Web Helper packages that allow you to easily integrate social networking features from Facebook, Twitter, Google, Microsoft Bing, and others into your web pages This book shows you how to rapidly develop web applications without the need to learn the complex concepts and highly structured programming models required by many other frameworks. Turn to Building ASP.NET Web Pages with Microsoft WebMatrix and get started with this easy-to-learn, easy-to-use, rapid web development platform. US $39.99 Shelve in .NET User level: Intermediate SOURCE CODE ONLINE www.apress.com For your convenience Apress has placed some of the front matter material after the index. Please use the Bookmarks Download from Wow! eBook <www.wowebook.com> and Contents at a Glance links to access them. Contents at a Glance Contents at a Glance .................................................................................................. iv Contents ..................................................................................................................... vi About the Author ...................................................................................................... xv About the Technical Reviewer ................................................................................. xvi Acknowledgments .................................................................................................. xvii Introduction ........................................................................................................... xviii Chapter 1: Introducing Microsoft WebMatrix ............................................................. 1 Chapter 2: Getting Started ........................................................................................ 11 Chapter 3: C# Primer ................................................................................................ 49 Chapter 4: Working with Razor and ASP.NET Web Pages ......................................... 95 Chapter 5: Forms and Validation ............................................................................ 129 Chapter 6: Working with Data ................................................................................ 171 Chapter 7: Security and Membership ..................................................................... 225 Chapter 8: Using Web Helper Packages .................................................................. 271 Chapter 9: TechieTogs: An Example E-commerce Site .......................................... 301 Chapter 10: TechieTogs: Adding E-commerce Features ........................................ 333 iv CONTENTS AT A GLANCE Chapter 11: TechieTogs: Site Administration and Finishing Touches ..................... 377 Chapter 12: Working with Images and Video ......................................................... 419 Chapter 13: Debugging, Caching, and Deployment ................................................ 443 Index ....................................................................................................................... 479 v C H A P T E R 1 ■ ■ ■ Introducing Microsoft WebMatrix WebMatrix is a web development platform from Microsoft aimed at providing a fast and lightweight way to develop dynamic web content. The WebMatrix platform consists of many individual component parts that go together to create a complete web development environment. Throughout the course of this book you will learn about each of these component parts in some detail. This chapter is not concerned with the inner-workings of the platform or its individual components, but is focused on explaining the landscape in which WebMatrix exists. It will aim to explain the intent behind the design of WebMatrix and the problems it sets out to solve. What is WebMatrix? WebMatrix is a set of free development tools that designed to allow users to easily create dynamic, data- driven web sites and publish them on the Internet. The tools and technologies included as part of the WebMatrix platform include among others, a database, web server, deployment tool, Search Engine Optimization (SEO) tool and a server-side scripting language. Although all of these tools and technologies are available for download and use as separate entities, Microsoft has combined them into a single stack known as WebMatrix. By doing this a web developer can control the development of all aspects of a web site from within a single environment, greatly reducing complexity. This reduction in complexity realizes many benefits, not least of which are the shortening of development times and a greatly reduced entry barrier – the developer must learn to use just one integrated environment, rather than five or six separate tools. To further reduce the learning curve, many of the component parts are simplified when presented as part of WebMatrix. For example, the web server supplied by Microsoft as part of WebMatrix is a smaller, development-friendly version of their full-blown Internet Information Services web server application and the database is the Compact Edition of SQL Server, their enterprise-level relational database server. This is a great advantage to new developers. WebMatrix presents you only with what you absolutely need to create web sites, cutting away the myriad of features that are used rarely, often only by advanced users. To the more advanced developer this simplification of the development environment can be a refreshing change, especially with the current trend of huge monolithic web development frameworks and architectures. You can after all still access the component parts through their native tools should the need arise. What WebMatrix offers above all else is simplicity, ease of use and rapid development. 1 CHAPTER 1 ■ INTRODUCING MICROSOFT WEBMATRIX What is a Dynamic, Data-Driven Web Site? So far we have learnt that Microsoft WebMatrix is a great tool for creating “dynamic, data-driven web sites”, but what exactly does that mean? Well let’s take the dynamic bit first. Dynamic Web Pages In simple terms website content can be provided in one of two ways. The first, more traditional way is to store the content in static files like documents and serve them whole to the client browser. If you were to take each page of this book and store it as an HTML page which was displayed back to the user in its entirety when they requested it, you would be providing static content. A static web page retrieved by different users at different times will always be displayed in exactly the same way. A dynamic page is one that freshly produces at least some of its content programmatically when a request is made to the web server. In other words, when a user requests a page from the web server some code is run which is used to construct the output to be returned to the user. This dynamic creation of content is the polar opposite of the traditional static website. WebMatrix uses ASP.NET Web Pages to execute C# or Visual Basic .NET code on the server to create dynamic content. Dynamic pages can also be used to accept input from users, which is then used for some kind of computation. For example, a page may request a monetary value in one currency, which is then converted to another currency on the server and the result displayed back to the user. Data-Driven Pages Data-driven pages are those that interact with some kind of data store. For example, input from the user may be used to create or update database records or data may be retrieved from an XML file and formatted by code for output. By using the data stores in database tables and records we are able to produce custom page content, whether on a user-by-user or some other basis. The typical database operations conducted by web pages are Create, Read, Update and Delete, typically abbreviated as CRUD. We can produce user interfaces and server-side code which will allow site users to carry out these operations on the database in a very controlled fashion. A Pragmatic Solution In reality most web pages produced by WebMatrix, or any of the other server-side dynamic frameworks such as PHP or Ruby on Rails, are a mix of static and dynamic content. This is done primarily for two reasons; speed and simplicity. It is much simpler for a developer to create static content and it is quicker to produce and serve. Therefore it is common practice to mix the two techniques within a single page, returning dynamic, data-driven content only where necessary, amongst the rest of the static markup. Figure 1-1 demonstrates this concept by identifying the areas of