Asp. New Code compilation in Net (i)

Source: Internet
Author: User
Asp.net| Compilation Summary: Learn how to use asp.net Whidbey to make it easier to work with code. The code directory automatically compiles codes for your site, but precompilation makes deployment easier.

Download the source code for this article. (Note that in the sample file, the programmer's comment is in English, which is translated into Chinese for easy Reader's understanding.) )


Brief introduction

The upcoming new version of Microsoft®asp.net introduced a number of features and improvements, its code name asp.net Whidbey, is based on the new version of the Microsoft®visual studio®.net name. Some of these features take advantage of the new features in the underlying microsoft®.net Framework version (ASP.net Whidbey is built on that version). Among these features, one of the most useful feature sets is related to code compilation. This article describes the major changes to the ASP.net Whidbey compilation model, the impact of these changes on writing asp.net applications, and how to take advantage of these changes.

The improved functionality and new compilation capabilities can be grouped into the following four basic areas:

1. Improvements to the modular code model.
2. The new Code directory.
3. New support for precompiled asp.net applications.
4.microsoft®intellisense® enhanced functionality.

New Modular Code Model

By default, sites developed using Visual Studio. NET 2002 or 2003 use a feature called "Modular Code" to separate visual elements (HTML tags, controls, and so on) from UI-related programming logic. When a developer creates a new Web form (such as foo.aspx), Visual Studio automatically creates an associated codebehind class file that has the same name as the Web form, followed by. vb or. cs (depending on the language in which the project is used). Class files are associated with Web forms through the codebehind and Inherits properties of the @ Page directive.

The class file contains the event-handling code, including the code used to bind the event handler to the corresponding event, and the detach declaration for each control (added to the. aspx file through the Visual Studio Web forms editor). When you compile (build) a Web application project, all of the codebehind classes in it are compiled into a. NET assembly that will be placed in the Web application's \ Bin directory. The Web Forms page itself is compiled dynamically at run time, and each Web form inherits from its associated codebehind class.

Although the original modular code model is theoretically good (who doesn't want to separate UI elements from programming logic)? ), but there are some drawbacks:

• Need to regenerate. In Visual Studio. NET, the runtime does not automatically compile the Codebehind class, so any changes to the Codebehind class need to regenerate the entire project to apply the changes. (Note that you can specify dynamic compilation of a modular code file by using the SRC attribute of the @ Page directive, but Visual Studio. NET does not perform this action by default.) )

• Shared development issues. Because all codebehind classes in the project are compiled into an assembly, it is difficult to have multiple developers develop a project at the same time without encountering bottlenecks.

• Code is easily corrupted. Controls exist in both declarations (in. aspx pages) and programmatically (in the Codebehind Class), and if the two sets of controls are not synchronized correctly, the code can easily be compromised.

• Increased complexity and lack of single file support. In Visual Studio. NET, many of the features that are used to improve productivity, including IntelliSense statement completion, require the use of modular code. Unfortunately, these features typically add a large amount of relatively complex code to the Codebehind class, which causes the code to be easily compromised because changing the code that is inserted by Visual Studio. NET can easily break the page.

With these drawbacks in mind, the team responsible for developing asp.net and Visual Studio. NET Whidbey decided to reconsider the modular code model. The new modular code model leverages the new capabilities of Microsoft®visual Basic®.net and C # called local classes (called local types in C #). A local class enables you to define different parts of a class in multiple files. At compile time, the compiler combines these parts together. ASP.net Whidbey uses the new Compilewith and Classname properties in the @ Page directive to identify the Codebehind local class to be combined with an. aspx page. By taking advantage of department classes and making other changes, the ASP.net team can accomplish the following:

• You do not need to write control declarations and event-binding code in the Codebehind class (binding events declaratively in a control declaration).

• Allows the runtime to dynamically compile both the Web Forms page and the Codebehind class without having to regenerate the entire project for minor changes.

• Reduce file contention in shared development.

• The same IDE experience is available for developers who use modular code files and developers who prefer single file development (all code and markup are included in. aspx files).

The following are the different views before and after the changes to the modular code model. The following code is the default code created by Visual Studio when you add a new Web Form using modular code (called a code-delimited Web Form in Visual Studio. NET Whidbey):

  
   
Visual Studio. NET 2002/2003webform1.aspx:<%@ Page language= "vb" autoeventwireup= "false" codebehind= " WebForm1.aspx.vb "inherits=" Testwebapp_121602.webform1 "%><! DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 transitional//en" >


Visual Studio. NET Whidbey

Default.aspx:

 
    
     
  <%@ page language= "VB" compilewith= "Default.aspx.vb"  classname= "ASP. Default_aspx "%>


Default.aspx.vb:

 
     
       Imports microsoft.visualbasicnamespace aspexpands Class default_aspxend classend Names Pace 
      


From the example above, it is clear that the code generated by Visual Studio. NET Whidbey is clearer and easier to read. You do not need to sacrifice drag-and-drop functionality or IntelliSense to accomplish this. Another cool and useful new feature of the

\code directory

asp.net Whidbey is the addition of the \code directory. The \code directory, similar to the \ Bin directory, is a special directory used by ASP.net, but it differs from the \ Bin directory: the \ Bin directory is used to store precompiled assemblies used by the application, and the \code directory is used to store class files to be dynamically compiled at run time. This allows you to store classes of business logic components, data access components, and other components in a location in your application and use them from any page. Because these classes are compiled dynamically at run time and are automatically referenced by an application that contains the \code directory, you do not need to build the project before you deploy the project, nor do you need to explicitly add a class reference. You can make changes to your components with confidence, and then deploy using a simple XCOPY or drag-and-drop operation. In addition to simplifying the deployment and referencing of components, the \code directory greatly simplifies the process of creating and accessing resource files (. resx) used during localization, as well as automating the generation and compilation of proxy classes for WSDL files (. wsdl). To better illustrate the implementation of the above actions, let's look at a few examples first. In the first example, we'll see how to create a simple business component and how to access it from a Web forms page.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.