Lập trình .net 4.0 và visual studio 2010 part 34 ppt

7 226 0
Lập trình .net 4.0 và visual studio 2010 part 34 ppt

Đang tải... (xem toàn văn)

Thông tin tài liệu

CHAPTER 10    225 ASP.NE T ASP.NET is a mature platform on which to build web applications, and it is probably fair to say that most of the changes in this release are enhancements and tweaks. However, don’t skip this chapter, because Microsoft has fixed a number of long-term omissions and introduced some very welcome changes. Project Templates VS2010 contains a number of new types of ASP.net project and some modifications to existing types: • New Empty ASP.NET Web Application (just the bare minimum) • New Silverlight 1.0 Web Site (erm, time to move on) • New ASP.NET AJAX Server Control and Server Control Extender • New ASP.NET MVC 2 Web Application • Modified ASP.NET Web Site and a modified ASP.NET Web Application (like VS2008’s ASP.NET Web Site project but with new changes in ASP.NET Web Application as discussed below) The ASP.NET Web projects now contain common authentication and profile functionality, a master page with a simple layout and jQuery scripts (see Figure 10-1). CHAPTER 10  ASP.NET 226 Figure 10-1. New Web Application Project template Web.config One of the aspects of ASP.NET that irritated me was how bloated Web.config files have become. To some extent, this bloat was due to ASP.NET 3.0 and 3.5 using version 2 of the CLR and, in order to introduce additional functionality, Microsoft had to override default settings, as well as cope with different versions of IIS (that is, settings for IIS 7.0 and settings for earlier versions). .NET 4.0, however, has a new version of the CLR, so this is no longer a problem. A number of settings have been moved to machine.config, which drastically reduces the size of Web.config. Functionality such as ASP.NET charting, dynamic data, Ajax, and routing is now also automatically registered for you, and so no longer has to be added to the Web.config file. Some developers may not like this addition of functionality that they may not use, but for the majority it will save a bit of configuration and can be overridden in machine.config. What follows is the default Web.config file that is generated for an ASP.NET 4.0 empty web project (the standard ASP.NET project has a larger Web.config as contains authentication and membership stuff as well). CHAPTER 10  ASP.NET 227 <?xml version="1.0"?> <configuration> <system.web> <compilation debug="true" targetFramework="4.0" /> </system.web> <system.webServer> <modules runAllManagedModulesForAllRequests="true"/> </system.webServer> </configuration> Much better. MULTITARGETING SUPPORT Please refer to Chapter 2 for details of multitargeting changes that will affect ASP.NET developers. ASP.NET developers should be aware that VS2010 has two different versions of the development web server: one for .NET 2.0 and the other for .NET 4.0 applications. IDE Changes Several enhancements have been made to the IDE and designer that will interest ASP.NET developers. • CSS 2.1 compliance has been improved. • Designer is now more robust and less likely to rewrite HTML (front page syndrome). • There is improved support for third-party libraries (in particular, jQuery). • There is better performance when working with large JavaScript files. • Intellisense support for different styles of JavaScript coding has been improved. • JavaScript documentation comments are parsed as you code. • There is Intellisense support for ASP.NET AJAX register namespaces calls. • Refactored IDE designer code will make it easier for Microsoft to improve the designer in the future jQuery I cover this in more detail in Chapter 12, but it is worth noting here that VS2010 comes bundled with the open source jQuery libraries and provides Intellisense support. When you create a new ASP.NET or ASP.NET MVC project, you will find many of the project templates include these libraries. CHAPTER 10  ASP.NET 228 Code Snippets Web development can sometimes be pretty repetitive. No, I am not talking about creating a lengthy online questionnaire that no sane person will ever fill out, but rather in terms of the markup you have to write. To ease this pain, Microsoft has introduced a number of prewritten HTML and ASP.NET snippets (blocks of code) that will substantially reduce the amount of typing you have to do and minimize typos (and possibly reduce RSI in the web development community). I am a big believer in saving time spent on tedious tasks, so let’s take a look into these snippets now. ASP.NET Code Snippets Let’s look at an example by following these steps. 1. Create a new ASP.NET project. 2. In Default.aspx, type “textbox” and then quickly press Tab. Visual Studio will insert the following code: <asp:TextBox runat="server" /> 3. But wait it gets better. Give your TextBox an ID of “txtTest “and press Enter to add a new line. 4. Type “requiredfieldvalidator” and press tab. 5. Visual Studio will then insert the markup below. Note how the ControlToValidate property was set to the nearest textbox automatically (if you add another textbox to the page you should still find this is the case), and how the cursor was focused on the ErrorMessage property . VS2010 will attempt to guess any properties it can (in this case due to the textbox’s proximity), and intelligently set the focus on any other fields that need to be filled in. <asp:RequiredFieldValidator ErrorMessage="errormessage" ControlToValidate="txtTest" runat="server" /> 6. The focus is currently set to the ErrorMessage property, so change it to “You must fill in this field” and then press Tab to move the focus to the next field.  NOTE Refer to Chapter 2 for details about how to create your own snippets. Using Snippets Web development snippets are divided into two types: ASP.NET and HTML. Most of the snippets are pretty much self explanatory, but in Table 10-1, I have described some of the more cryptic ones. CHAPTER 10  ASP.NET 229 Table 10-1. ASP.NET Code Snippets Short cut/trigger key N Note s Checkbox Formr Server-side form hyperlink Image Label listbox listitem listview loginname loginstatus loginview multiview panel placeholder radiobutton register Custom control register directive registerascx User control register directive repeater requiredfieldvalidator runat scriptreference scripts servicereference sitemappath sm ScriptManager (Continued) CHAPTER 10  ASP.NET 230 Table 10-1. Continued Short cut/trigger key N Note s smp ScriptManagerProxy sqldatasource textbox TextBox updatepanel updateprogress validationsummary view There are also a number of HTML snippets available. I would like to draw your attention to the XHTML doctype snippets as I believe these are particularly useful, shown Table 10-2. Table 10-2. HTML Snippets Short cut Note s a br class div form hr html html4f HTML 4.01 frameset doctype declaration html4s HTML 4.01 strict doctype declaration html4t HTML 4.01 transitional doctype declaration img input link metaie8 IE compatibility flag: <meta http-equiv="X-UA- Compatible" content="IE=8" /> (Continued) CHAPTER 10  ASP.NET 231 Table 10-2. Continued Sh or t c ut No t e s script Client script block scriptr Server script block scriptref Client script reference select span style table ul xhtml10f XHTML 1 frameset declaration xhtml10s XHTML 1.0 Strict document declaration xhtml10t XHTML 1.0 Transitional document declaration xhtml11 XHTML 1.1 doctype Deployment I think one of the biggest and most valuable changes in ASP.NET 4.0 is the new deployment functionality. VS2010 makes it easier than ever before deploy your ASP.NET application. VS2010 allows you to • Perform transformations on Web.config for different build configurations. • Create web packages (zip files that contain your application, database, and settings in a single file). • Install your application and all its settings with just one click (one-click publishing). Automating your deployment processes is a smart move, because it reduces mistakes, saves you time, and creates a repeatable and self-documented process. Web.config Transformation Many developers use Web.config file to hold application specific settings, such as database connection strings. The trouble with Web.config is that when you deploy your applications, you normally have to change these settings. VS2010 offers the ability to create a build configuration that allows you to modify the contents of the Web.config file for different build configurations. Note you can also use this feature on Web.config files in sub folders. VS2010 actually uses this functionality already when you switch between debug/release mode, let’s take a look at this now. . will affect ASP .NET developers. ASP .NET developers should be aware that VS 201 0 has two different versions of the development web server: one for .NET 2 .0 and the other for .NET 4. 0 applications and most valuable changes in ASP .NET 4. 0 is the new deployment functionality. VS 201 0 makes it easier than ever before deploy your ASP .NET application. VS 201 0 allows you to • Perform transformations. are particularly useful, shown Table 10- 2. Table 10- 2. HTML Snippets Short cut Note s a br class div form hr html html4f HTML 4. 01 frameset doctype declaration html4s HTML 4. 01 strict

Ngày đăng: 01/07/2014, 21:20

Từ khóa liên quan

Tài liệu cùng người dùng

Tài liệu liên quan