Asp.Net Web Pages Using the Razor Syntax
Total Page:16
File Type:pdf, Size:1020Kb
ASP.NET WEB PAGES USING THE RAZOR SYNTAX Microsoft® ASP.NET Web Pages is a free Web development technology that is designed to deliver the world's best experience for Web developers who are building websites for the Internet. This book provides an overview of how to create dynamic Web content using ASP.NET Web Pages with the Razor syntax. Note This document is preliminary documentation for the Beta release of Microsoft WebMatrix and ASP.NET Web pages and is subject to change. For the latest information, visit http://www.asp.net/webmatrix. Chapter 1 - Getting Started with Microsoft WebMatrix Beta and ASP.NET Web Pages 1 What is WebMatrix Beta? 1 Installing WebMatrix Beta 1 Getting Started with WebMatrix Beta 2 Using ASP.NET Web Pages Code 6 Creating and Testing ASP.NET Pages Using Your Own Text Editor 8 Chapter 2 - Introduction to ASP.NET Web Programming Using the Razor Syntax 10 The Top 8 Programming Tips 10 HTTP GET and POST Methods and the IsPost Property 16 A Simple Code Example 16 Basic Programming Concepts 18 Classes and Instances 19 Language and Syntax 20 HTML Encoding 21 Additional Resources 38 Chapter 3 - Creating a Consistent Look 39 Creating Reusable Blocks of Content 39 Creating a Consistent Look Using Layout Pages 42 Designing Layout Pages That Have Multiple Content Sections 45 Making Content Sections Optional 48 Passing Data to Layout Pages 49 Chapter 4 - Working With Forms 54 Creating a Simple HTML Form 54 Reading User Input From the Form 55 HTML Encoding for Appearance and Security 57 Validating User Input 58 Restoring Form Values After Postbacks 59 Additional Resources 61 Chapter 5 - Working With Data 62 Introduction to Databases 62 Relational Databases 63 Creating a Database 63 Adding Data to the Database 65 Displaying Data from a Database 65 Structured Query Language (SQL) 67 Inserting Data in a Database 68 Updating Data in a Database 71 Deleting Data in a Database 76 Displaying Data Using the WebGrid Helper 79 Connecting to a Database 81 Additional Resources 82 Chapter 6 - Working With Files 83 Creating a Text File and Writing Data to It 83 Appending Data to an Existing File 86 Reading and Displaying Data from a File 87 Displaying Data from a Microsoft Excel Comma-Delimited File 89 Deleting Files 89 Letting Users Upload a File 91 Letting Users Upload Multiple Files 94 Additional Resources 96 Chapter 7 - Working With Images 97 Adding an Image to a Web Page Dynamically 97 Uploading an Image 99 About GUIDs 102 Resizing an Image 102 Rotating and Flipping an Image 104 Adding a Watermark to an Image 106 Using an Image As a Watermark 107 Chapter 8 - Working with Video 109 Choosing a Video Player 109 MIME Types 110 Playing Flash (.swf) Videos 110 Playing MediaPlayer (.wmv) Videos 112 Playing Silverlight Videos 114 Additional Resources 115 Chapter 9 - Adding Email to Your Website 116 Sending Email Messages from Your Website 116 Sending a File Using Email 119 Additional Resources 122 Chapter 10 - Adding Social Networking to Your Website 123 Linking Your Website on Social Networking SItes 123 Adding a Twitter Feed 124 Rendering a Gravatar Image 126 Displaying an Xbox Gamer Card 127 Chapter 11 - Analyzing Traffic 129 Tracking Visitor Information (Analytics) 129 Chapter 12 - Caching to Improve the Performance of Your Website 132 Caching to Improve Website Responsiveness 132 Chapter 13 – Adding Security and Membership 135 Introduction to Website Membership 135 Creating a Website That Has Registration and Login Pages 136 Creating a Members-Only Page 139 Creating Security for Groups of Users (Roles) 141 Creating a Password-Change Page 143 Letting Users Generate a New Password 144 Preventing Automated Programs from Joining Your Website 148 Chapter 14 - Introduction to Debugging 151 Using the ServerInfo Helper to Display Server Information 151 Embedding Output Expressions to Display Page Values 153 Using the ObjectInfo Helper to Display Object Values 156 Using Debugging Tools 158 Examining Traffic Using the WebMatrix Beta Requests Tool 160 Analyzing SEO Using the Reports Workspace 162 Additional Resources 164 Chapter 15 - Customizing Site-Wide Behavior 165 Adding Website Startup Code 165 Running Code Before and After Files in a Folder 169 Creating More Readable and Searchable URLs 173 Appendix A - API Quick Reference 177 Introduction 177 Classes 177 Data 180 Helpers 181 Appendix B - Visual Basic Language and Syntax 185 The Top 8 Programming Tips 185 HTTP GET and POST Methods and the IsPost Property 191 A Simple Code Example 191 Visual Basic Language and Syntax 193 HTML Encoding 194 Additional Resources 211 Chapter 1 - Getting Started with Microsoft WebMatrix Beta and ASP.NET Web Pages This chapter introduces Microsoft WebMatrix Beta, a free Web development technology that delivers the world's best experience for Web developers. What you'll learn What is WebMatrix? How to install WebMatrix. How to get started creating a simple website using WebMatrix. How to create a dynamic Web page using WebMatrix. What is WebMatrix Beta? WebMatrix is a free, lightweight set of Web development tools that provides the easiest way to build websites. It includes IIS Developer Express (a development Web server), ASP.NET (a Web framework), and Microsoft SQL Server® Compact (an embedded database). It also includes a simple tool that streamlines website development and makes it easy to start websites from popular open source apps. The skills and code you develop with WebMatrix transition seamlessly to Microsoft Visual Studio® and SQL Server. The web pages that you create using WebMatrix can be dynamic—that is, they can alter their content or style based on user input or on other information, such as database information. To program dynamic Web pages, you use ASP.NET with the Razor syntax and the C# or Microsoft Visual Basic® programming languages. If you already have programming tools that you like, you can try the WebMatrix tools or you can use your own tools to create websites that use ASP.NET. This chapter shows you how WebMatrix makes it easy to get started creating websites and dynamic web pages. Installing WebMatrix Beta To install WebMatrix, you can use Microsoft’s Web Platform Installer, which is a free application that makes it easy to install and configure web-related technologies. 1. If you don't already have the Web Platform Installer, download it from the following URL: http://www.microsoft.com/web/downloads/platform.aspx ASP.NET Web Pages: Chapter 1 - Getting Started with Microsoft WebMatrix Beta and ASP.NET Web Pages 1 © Microsoft. All Rights Reserved 2. Run the Web Platform Installer, select Getting Started, and then click Install Now. Getting Started with WebMatrix Beta To begin, you'll create a new website and a simple web page. 1. Start WebMatrix. ASP.NET Web Pages: Chapter 1 - Getting Started with Microsoft WebMatrix Beta and ASP.NET Web Pages 2 © Microsoft. All Rights Reserved 2. Click Site From Template. Templates include prebuilt files and pages for different types of websites. 3. Select Empty Site and name the new site HelloWorld. 4. Click OK. WebMatrix creates and opens the new site. At the top, you see a Quick Access Toolbar and a ribbon, as in Microsoft Office 2010. At the bottom left, you see the workspace selector, which contains buttons that determine what appears above them in the left pane. On the right is the content pane, which is where you view reports, edit files, and so on. Finally, across the bottom is the notification bar, which shows messages as needed. ASP.NET Web Pages: Chapter 1 - Getting Started with Microsoft WebMatrix Beta and ASP.NET Web Pages 3 © Microsoft. All Rights Reserved 5. Select the Files workspace. This workspace lets you work with files and folders. The left pane shows you the file structure of your site. 6. In the ribbon, click New and then click New File…. WebMatrix displays a list of file types. Most of these are probably familiar, like HTML, CSS, and TXT. ASP.NET Web Pages: Chapter 1 - Getting Started with Microsoft WebMatrix Beta and ASP.NET Web Pages 4 © Microsoft. All Rights Reserved 7. Select CSHTML, and in the Name box, type HelloWorld.cshtml. A CSHTML page is a special type of page in WebMatrix that can contain the usual contents of a web page, such as HTML and JavaScript code, and that can also contain code for programming web pages. (You'll learn more about CSHTML files later.) 8. Click OK. WebMatrix creates the page and opens it in the editor. As you can see, this is ordinary HTML markup. 9. Add the following highlighted HTML and content to the page: <!DOCTYPE html> <html> <head> <title>Hello World Page</title> </head> <body> <h1>Hello World Page</h1> <p>Hello World!</p> </body> </html> 10. In the Quick Access Toolbar, click Save. 11. In the ribbon, click Run. ASP.NET Web Pages: Chapter 1 - Getting Started with Microsoft WebMatrix Beta and ASP.NET Web Pages 5 © Microsoft. All Rights Reserved 12. WebMatrix starts a web server (IIS Developer Express) that you can use to test pages on your computer. The page is displayed in your default browser. In the next section, you'll see how easy is it is to add code to the HelloWorld.cshtml page in order to create a dynamic page. Using ASP.NET Web Pages Code In this procedure, you'll create a page that uses simple code to display the server date and time on the page. The example here will introduce you to the Razor syntax that lets you embed code into the HTML on ASP.NET Web pages. (You can read more about this in the next chapter.) The code introduces a helper, which is a powerful concept in ASP.NET.