Professional VB 2005 with .NET 3.0 (Programmer to Programmer)
The evolution of ASP.NET continues! The progression from Active Server Pages 3.0 to ASP.NET 1.0 was revolutionary, to say the least, and we are here to tell you that the evolution from ASP.NET 1.0/1.1 to ASP.NET 2.0 is just as exciting and dramatic.
The introduction of ASP.NET 1.0/1.1 changed the Web programming model; but ASP.NET 2.0 is just as revolutionary in the way it increases productivity. The primary goal of ASP.NET 2.0 is to enable you to build powerful, secure, and dynamic applications using the least possible amount of code. Although this book covers the new features provided by ASP.NET 2.0, it also covers most of what the ASP.NET technology offers.
The Goals of ASP.NET 2.0
ASP.NET 2.0 is a major release of the product and an integral part of the .NET Framework 2.0. This release of ASP.NET heralds a new wave of development that should eliminate any of the remaining barriers to adopting this new way of coding Web applications.
When the ASP.NET team started working on ASP.NET 2.0, it had specific goals to achieve. These goals focused around developer productivity, administration and management, as well as performance and scalability. These goals are achieved with this milestone product release.
Developer Productivity
Much of the focus of ASP.NET 2.0 is on productivity. Huge productivity gains were made with the release of ASP.NET 1.x - could it be possible to expand further on those gains?
One goal the development team had for ASP.NET 2.0 was to eliminate much of the tedious coding that ASP.NET originally required and to make common ASP.NET tasks easier. The team developing ASP.NET 2.0 had the goal of reducing by two-thirds the number of lines of code required for an ASP.NET application. It succeeded; you will be amazed at how quickly you can create your applications in ASP.NET 2.0.
Administration and Management
The initial release of ASP.NET focused on the developer, and little thought was given to the people who had to administer and manage all the ASP.NET applications that were built and deployed. Instead of working with consoles and wizards as they did in the past, administrators and managers of these new applications now had to work with unfamiliar XML configuration files such as machine.config and web.config.
To remedy this situation, ASP.NET 2.0 now includes a Microsoft Management Console (MMC) snap-in that enables Web application administrators to edit configuration settings easily on-the-fly through IIS.
Performance and Scalability
One of the goals for ASP.NET 2.0 set by the Microsoft team was to provide the world’s fastest Web application server. This book addresses a number of performance enhancements available in ASP.NET 2.0.
One of the most exciting performance enhancements is the new caching capability aimed at exploiting Microsoft’s SQL Server. ASP.NET 2.0 now includes a feature called SQL cache invalidation. Before ASP.NET 2.0, it was possible to cache the results that came from SQL Server and to update the cache based on a time interval - for example, every 15 seconds or so. This meant that the end user might see stale data if the result set changed sometime during that 15-second interval.
In some cases, this time interval result set is unacceptable. In an ideal situation, the result set stored in the cache is destroyed if any underlying change occurs in the source from which the result set is retrieved - in this case, SQL Server. With ASP.NET 2.0, you can make this happen with the use of SQL cache invalidation. When the result set from SQL Server changes, the output cache is triggered to change, and the end user always sees the latest result set. The data presented is never stale.
Other big areas of change in ASP.NET are performance and scalability. ASP.NET 2.0 now provides 64-bit support, which means you can now run your ASP.NET applications on 64-bit Intel or AMD processors. In addition, because ASP.NET 2.0 is fully backwardly compatible with ASP.NET 1.0 and 1.1, you can now take any former ASP.NET application, recompile it on the .NET Framework 2.0, and run it on a 64-bit processor.
New Compilation System
In ASP.NET 2.0, code is constructed and compiled in a new way. Compilation in ASP.NET 1.0 was always a tricky scenario. With ASP.NET 1.0, you could build an application’s code-behind files using ASP.NET and Visual Studio, deploy it, and then watch as the .aspx files were compiled page by page as each page was requested. If you made any changes to the code-behind file in ASP.NET 1.0, it was not reflected in your application until the entire application was rebuilt. That meant that the same page-by-page request had to be done again before the entire application was recompiled.
Everything regarding how ASP.NET 1.0 worked with classes and compilation changed with the release of ASP.NET 2.0. The mechanics of the new compilation system actually begin with how a page is structured. In ASP.NET 1.0, you constructed your pages either by using the code-behind model or by placing all the server code inline between <script> tags on your .aspx page. Most pages were constructed using the code-behind model because this was the default when using Visual Studio .NET 2002 or 2003. It was quite difficult to create your page using the inline style in these IDEs. If you did, you were deprived of the use of IntelliSense, which can be quite the lifesaver when working with the tremendously large collection of classes that the .NET Framework offers.
ASP.NET 2.0 offers a new code-behind model because the .NET Framework 2.0 offers the capability to work with partial classes (also called partial types). Upon compilation, the separate files are combined into a single offering. This gives you much cleaner code-behind pages. The code that was part of the Web Form Designer Generated section of your classes is separated from the code-behind classes that you create yourself. Contrast this with the ASP.NET 1.0 .aspx file’s need to derive from its own code-behind file to represent a single logical page.
ASP.NET 2.0 applications can include an \App_Code directory in which you place your class’s source. Any class placed here is dynamically compiled and reflected in the application. Unlike ASP.NET 1.0, you do not use a separate build process when you make changes. This enables a “just save and hit” deployment model like the one in classic ASP 3.0. Visual Studio Web Developer also automatically provides IntelliSense for any objects that are placed in the \App_Code directory, whether you are working with the code-behind model or are coding inline.
ASP.NET 2.0 also provides you with tools that enable you to precompile your ASP.NET applications - both .aspx pages and code behind - so that no page within your application has latency when it is retrieved for the first time. This is also a great way to discover any errors in the pages without invoking every page. Precompiling your ASP.NET 2.0 applications is as simple as using aspnet_compiler.exe and employing some of the available flags. As you precompile your entire application, you receive error notifications if any errors are found anywhere within it. Precompilation also enables you to deliver only the created assembly to the deployment server, thereby protecting your code from snooping, unwanted changes, and tampering after deployment.
Health Monitoring for Your ASP.NET Applications
The built-in health-monitoring capabilities are new and rather significant features designed to make it easier to manage a deployed ASP.NET application. Health monitoring provides what the term implies - the capability to monitor the health and performance of your deployed ASP.NET applications.
ASP.NET health monitoring is built around various health-monitoring events (which are referred to as Web events) occurring in your application. Using the new health monitoring system enables you to perform event logging for Web events such as failed logins, application starts and stops, or any unhandled exceptions. The event logging can occur in more than one place, so you can log to the Event Log or even back to a database. In addition to performing this disk-based logging, you can also use the system to e-mail health-monitoring information.
Besides working with specific events in your application, you can use the health-monitoring system to take health snapshots of a running application. As with most systems built into ASP.NET 2.0, you can extend the health-monitoring system and create your own events for recording application information.
Reading and Writing Configuration Settings
Using the WebConfigurationManager class, you have the capability to read and write to the server or application configuration files. This means that you can write and read settings in the machine.config or the web.config files that your application uses.
The capability to read and write to configuration files is not limited to working with the local machine in which your application resides. You can also perform these operations on remote servers and applications.
Of course, a GUI way exists for performing these read or change operations on the configuration files at your disposal. Most exciting, however, is that the built-in GUI tools that provide this functionality (such as the new ASP.NET MMC snap-in) use the WebConfigurationManager class that is also available for building custom administration tools.
Localization
ASP.NET 2.0 improves upon previous versions by making it easier to localize applications. In addition to using Visual Studio, you can create resource files (.resx) that enable you to dynamically change the pages you create based upon the culture settings of the requester.
ASP.NET 2.0 also provides the capability to provide resources application-wide or just to particular pages in your application through the use of two new application folders - App_GlobalResources and App_LocalResources.
The items defined in any .resx files you create are then accessible directly in the ASP.NET server controls or programmatically, using expressions such as the following:
<%= Resources.Resource.Question %>
This new system is straightforward and simple to implement.
New Objects for Accessing Data
One of the more code-intensive tasks in ASP.NET 1.0 was the retrieval of data. In many cases, this meant working with a number of objects. If you have been working with ASP.NET for a while, you know that it is an involved process to display data from a Microsoft SQL Server table within a DataGrid server control. For instance, you first had to create a number of new objects, including a SqlConnection object followed by a SqlCommand object. When those objects were in place, you then created a SqlDataReader to populate your DataGrid by binding the result to the DataGrid. In the end, a table appeared containing the contents of the data you were retrieving (such as the Customers table from the Northwind database).
ASP.NET 2.0 eliminates this intensive procedure with the introduction of a new set of objects that work specifically with data access and retrieval. These new data controls are so easy to use that you access and retrieve data to populate your ASP.NET server controls without writing any code. Even better, this new functionality is not limited to Microsoft’s SQL Server. In fact, several data source server controls are at your disposal, and you can create your own. In addition to the SqlDataSource server control, ASP.NET 2.0 introduces the AccessDataSource, XmlDataSource, ObjectDataSource, and SiteMapDataSource server controls.
New Server Controls
So far, you have seen a number of new server controls that you can use when building your ASP.NET 2.0 pages. For example, the preceding section described the new data source server controls that you can use to access different kinds of datastores. You also saw the use of the new GridView server control, which is an enhanced version of the previous DataGrid control used in ASP.NET 1.0.
ASP.NET 2.0 provides more than 50 additional new server controls! In fact, so many new server controls have been introduced that the next IDE for building ASP.NET applications, Visual Studio 2005, had to reorganize the Toolbox in which all the server controls are stored. They are now separated into categories, unlike the list used in Visual Studio .NET or the ASP.NET Web Matrix. The new Visual Studio 2005 Toolbox is shown in Figure 19-1.
Категории