Asp.net is not a simple ASP upgrade, but Microsoft. an important part of the net plan, which relies on. net multi-language and powerful class library support, introduced the Server HTML control and web control, automatically process the control of the client and the server
Provides developers with a window programming interface similar to Windows.ProgramFunctions provide good programming interfaces and greatly improve the efficiency of developers.
However, the "one-time conversion and two-time compilation" process makes the aspx file slightly inadequate when it is first executed (or run for the first time after the update), especially when it has a large number of aspx and codebehindCodeIn the application environment of the file, compile the aspx file into a DLL (in. net, called Application assembly) and then released, saving the time for "one conversion, one compilation" and CPU usage, which will greatly improve the overall performance of Web Services. Of course, after being compiled into a DLLSource codeThe confidentiality is also improved to a certain extent.
This article through ASP. the basic processing process of Net and the analysis of an accidental secret are introduced in ASP. net, how to establish aspx to DLL ing, how to develop a DLL that can handle HTTP requests/responses, and how to set "traps ", compile a single off-the-shelf aspx file and the codebehind aspx file into a DLL,ArticleFinally, I will introduce some tips for the actual operation.
This article involves ASP. NET application, command line compilation, Web. config configuration file and other concepts, in order to make readers better understand the content of this article, but also to make this article not seem cumbersome, first introduce the system environment corresponding to this article:
System Environment:
Win2000 (SP3) + iis5 +. NET Framework 1.0 (Chinese version ).
Server Name:
As the examples in this article are all tested on the local machine, the server name is localhost.
IIS settings:
Create the virtual directory dlltest (the actual path is W: \ wwwroot \ dlltest), set it as an application, and create the bin directory under dlltest. All source files are stored in the dlltest directory, and all DLL files are stored in the dlltest \ bin directory.
ASP. NET application configuration file -- web. config
Create a web. config file in the dlltest directory. The content of the file is as follows:
<? XML version = "1.0"?>
<Configuration>
<System. Web/>
</Configuration>
Command window (DOS window)
Open the command window and run the CD command to make the current directory W: \ wwwroot \ dlltest.
1. Create a DLL ing from aspx to DLL
First, let's take a look at how the aspx file is processed by ASP. NET in general:
When an HTTP request (such as "http: // webserver/webapp/webpage. when the request is sent from the client to the IIS server, IIS captures and analyzes the request. When it analyzes that the request is An ASPX page, it immediately calls the asp.netruntime environment (aspnet_wp.exe), Asp. check "/webapp/webpage. whether aspx exists. If not, the HTTP 404 (File not found) error is returned to the client. Otherwise. find the corresponding DLL file in the temporary directory of. net. If the DLL does not exist or is "old" than the aspx source file, the compiler is called (if the aspx server script language is VB or JScript, then, call the corresponding vbc compiler, JSC compiler) to compile the aspx file into a DLL, and then ASP. net then calls the DLL to process specific customer requests and return the server response.
From this process, we can see that the ASP. NET runtime environment will automatically identify, check, and update the DLL corresponding to Aspx. Is there any other way to forcibly route the processing of An ASPX file to a compiled DLL? In the httphandlers section of the system. web section of the web. config file of the ASP. NET application configuration file, add the ing items from aspx to DLL. The syntax is as follows:
<Add verb = "*" Path = "aspx file name" type = "Class Name, DLL file"/>
Aspx file: Virtual name to be "routed". The extension must be Aspx. Otherwise, IIS will process the file before the ASP. NET runtime environment.
DLL file: the name of the DLL file (Application assembly). You do not need to enter ". dll ". ASP. NET first searches for the Assembly DLL in the dedicated \ bin directory of the application, and then searches for the Assembly DLL in the system assembly cache.
Class Name: because a DLL may have multiple namespaces or classes, you must specify the class automatically loaded when the DLL is called.
For example, the web. config file of an ASP. NET application is as follows:
<? XML version = "1.0"?>
<Configuration>
<System. Web>
<Httphandlers>
<Add verb = "*" Path = "index. aspx" type = "bbs. indexpage, BBS"/>
</Httphandlers>
</System. Web>
</Configuration>
The configuration file tells ASP. net, in the client request the index of the application. when the aspx file is used, the BBS under the application bin directory is called directly. DLL, and automatically load the BBS. indexpage class.
2. develop DLL for processing HTML pages
It should be noted that not all application Assembly dll can implement the HTTP Request/response mode. Let's take a look at the descriptions of HTTP processing programs and factories in Microsoft ASP. NET Quick Start (http://chs.gotdotnet.com/quickstart/aspplus:
ASP. NET provides low-level request/response APIs, allowing developers to use the. NET Framework class to provide services for incoming HTTP requests. Therefore, developers need to create classes that support the system. Web. ihttphandler interface and processrequest () method. The processing program is usually useful when processing HTTP requests without services abstracted by a high-level page framework. Common uses of handlers include filters and CGI-like applications, especially those that return binary data.
Each HTTP request received by ASP. NET is finally processed by a specific instance of the ihttphandler class. Ihttphandlerfactory provides the structure for actual resolution of ihttphandler instance URL requests. In addition to the default ihttphandlerfactory class provided by ASP. NET, developers can also choose to create and register factories to support a large number of Request Parsing and activation schemes.
It can be seen from this text that when the ASPX page is not involved. net Framework provides advanced interface technology (such as data caching, status persistence, web form Control Reference, etc.), and the output to the client is not complex HTML text, in particular, when you only return binary data (slice, sound, etc.) to the client, you can use one. CS application file (This article uses the C # language, if it is VB or JScript ,...) and the application must have an implementation system. web. ihttphandler interface and class that implements the processrequest () method. A simple example is as follows:
/* Source File: ex1.cs start */
Using system. Web;
Namespace dlltest
{
/*
Class must implement the ihttphandler interface. If the program accesses the session state (Session), The irequiressessionstate interface must be implemented (the flag interface that does not contain any methods ).
*/
Public class ex1page: ihttphandler
{
/*
The isreusable attribute tells the. NET Framework whether the program can be used by multiple threads at the same time.
True corresponds to yes; false corresponds to whether or not.
*/
Public bool isreusable
{
Get {return true ;}
}
/*
Implement the processrequest method to return response data to the client.
In this example, a simple HTML page is returned to the client.
*/
Public void processrequest (httpcontext context)
{
Httpresponse res = context. response;
Res. Write ("<HTML> <body> ");
Res. Write ("
Res. Write ("this page is directly processed by DLL ");
Res. Write ("
}
}
}
/* Source File: ex1.cs ended */
In the command line status, use the following compilation command to compile ex1.cs into ex1.dll and store it in the bin directory.
T: Library/out: bin \ ex1.dll ex1.cs
Add aspx-> DLL ing to the configuration file web. config. After adding the map, Web. config should be like this:
<? XML version = "1.0"?>
<Configuration>
<System. Web>
<Httphandlers>
<Add verb = "*" Path = "dlltest1.aspx" type = "dlltest. ex1page, ex1"/>
</Httphandlers>
</System. Web>
</Configuration>
Now, when the browser accesses http: // localhost/dlltest/dlltest1.aspx, it actually calls the processrequest method of the dlltest. ex1page class in ex1.dll. You can see a simple page during browsing.
3. Compile a single aspx file into a DLL
Microsoft does not allow developers to directly compile the aspx file into a DLL from the "Implication" that Microsoft has publicly described in the previous section. However, Asp. net Advanced Interface Technology (server-side HTML controls, Web controls, and so on) All need to be presented through the aspx file. If the advanced features of aspx are abandoned for DLL running efficiency, it is obvious that you have never had to give it a try.
Now let's take a look:
The compiler is only a C # Language compiler. It can only compile files that comply with the C # language specification, while the aspx file format obviously does not comply with the C # language specification, therefore, the compiler cannot compile the aspx source file.
Therefore, to compile the aspx file into a DLL file, you must first convert the aspx file into a Cs source file that the compiler can recognize. So what tools are used for conversion? Although I believe that this tool must be hidden. net Framework, but a large number of ASP. net and.. net.
Oh, there is no path to death, and I am still able to discover this secret by chance.
Let's take a look at the source file ex2.aspx:
/* Source File: ex2.aspx start */
<% @ Page Language = "C #" %>
<SCRIPT runat = "server">
/*
You are not mistaken. The next line is "abcdefg", which gives me the opportunity to write this article ^_^;
In this article, I call this line a "code trap"
*/
Abcdefg // code trap
Void page_load (Object SRC, eventargs ARGs)
{
If (! Ispostback) notelabel. Text = "enter your name :";
}
Void onnamesubmit (Object SRC, eventargs ARGs)
{
String name = f_name.value;
Notelabel. Text = (name = "")? "Name cannot be blank": Name + ", hello. Welcome! ";
}
</SCRIPT>
<HTML>
<Body>
<Form runat = "server">
<H1> dlltest-ex2 (Example 2)
<HR>
<Asp: Label runat = "server" id = "notelabel" style = "color: red; font-weight: bold"/>
<Input runat = "server" id = "f_name" size = "8">
<Button runat = "server" onserverclick = "onnamesubmit"> OK </button>
</Form>
</Body>
</Html>
/* Source File: ex2.aspx ended */
If you comment out or delete the "Code trap", ex2.aspx is a simple ASP. NET file. You can use IE to browse this page and find that it works properly.
Now let's open the "trap" to see what ASP. NET returns?
A "Compilation error" page is returned, and the report source file cannot be compiled. We are interested in a hyperlink at the bottom of the page named "show complete compilation source". Click some links, you can see the complete content of the Cs source file ("Complete Compilation source") converted from ex2.aspx. Remove the preceding line number information from the "Complete Compilation source" and some other compilation switches (mainly # line compilation commands ), and close the cute "code trap" (use // to comment it out or delete it directly) and save it as ex2_aspx.cs:
/* Source File: ex2_aspx.cs start */
/*
It can be seen from the following illustration that there is indeed an undisclosed tool to convert the aspx file into the Cs source file.
*/
//------------------------------------------------------------------------------
// <Autogenerated>
// This code was generated by a tool.
// Runtime version: 1.0.3705.0
//
// Changes to this file may cause incorrect behavior and will be lost if
// The code is regenerated.
/// </Autogenerated>
//------------------------------------------------------------------------------
/*
The strange thing is that the namespace is actually ASP rather than Aspx.
We recommend that you change the name to a suitable application name to prevent name conflicts. For example, you can change the name to dlltest for this article.
I didn't change it here to make it clear to everyone.
*/
Namespace ASP {
Using system;
Using system. collections;
Using system. Collections. Specialized;
Using system. configuration;
Using system. text;
Using system. Text. regularexpressions;
Using system. Web;
Using system. Web. caching;
Using system. Web. sessionstate;
Using system. Web. Security;
Using system. Web. UI;
Using system. Web. UI. webcontrols;
Using system. Web. UI. htmlcontrols;
/*
1. Pay attention to the composition of the class name. If necessary, you can change it to a meaningful name. For example, you can change it to ex2page for this article.
2. Pay attention to its base class. Javase. Web. UI. Page implements the ihttphandler interface. To access the session, the irequiressessionstate interface is also implemented.
*/
Public class ex2_aspx: system. Web. UI. Page, system. Web. sessionstate. irequiressessionstate {
Private Static int _ autohandlers;
Protected system. Web. UI. webcontrols. Label notelabel;
Protected system. Web. UI. htmlcontrols. htmlinputtext f_name;
Protected system. Web. UI. htmlcontrols. htmlbutton _ control3;
Protected system. Web. UI. htmlcontrols. htmlform _ control2;
Private Static bool _ intialized = false;
Private Static system. Collections. arraylist _ filedependencies;
/* Now you can turn off the "trap */
// Abcdefg
Void page_load (Object SRC, eventargs ARGs)
{
If (! Ispostback) notelabel. Text = "enter your name :";
}
Void onnamesubmit (Object SRC, eventargs ARGs)
{
String name = f_name.value;
Notelabel. Text = (name = "")? "Name cannot be blank": Name + ", hello. Welcome! ";
}
/* Constructor */
Public ex2_aspx (){
System. Collections. arraylist dependencies;
If (Asp. ex2_aspx. _ intialized = false )){
Dependencies = new system. Collections. arraylist ();
/*
Comment out the following line to make the DLL a dependency-free independent file.
Prevent searching and comparing the New and Old dependency files when the DLL is running.
*/
// Dependencies. Add ("W: \ wwwroot \ dlltest \ ex2.aspx ");
ASP. ex2_aspx. _ filedependencies = dependencies;
ASP. ex2_aspx. _ intialized = true;
}
}
Protected override int autohandlers {
Get {
Return ASP. ex2_aspx. _ autohandlers;
}
Set {
ASP. ex2_aspx. _ autohandlers = value;
}
}
Protected system. Web. httpapplication applicationinstance {
Get {
Return (system. Web. httpapplication) (This. Context. applicationinstance ));
}
}
Public override string templatesourcedirectory {
Get {
Return "/dlltest ";
}
}
Private system. Web. UI. Control _ buildcontrolnotelabel (){
System. Web. UI. webcontrols. Label _ CTRL;
_ CTRL = new system. Web. UI. webcontrols. Label ();
This. notelabel = _ CTRL;
_ Ctrl. ID = "notelabel ";
(System. Web. UI. iattributeaccessor) (_ CTRL). setattribute ("style", "color: red; font-weight: bold ");
Return _ CTRL;
}
Private system. Web. UI. Control _ buildcontrolf_name (){
System. Web. UI. htmlcontrols. htmlinputtext _ CTRL;
_ CTRL = new system. Web. UI. htmlcontrols. htmlinputtext ();
This. f_name = _ CTRL;
_ Ctrl. ID = "f_name ";
_ Ctrl. size = 8;
Return _ CTRL;
}
Private system. Web. UI. Control _ buildcontrol _ control3 (){
System. Web. UI. htmlcontrols. htmlbutton _ CTRL;
_ CTRL = new system. Web. UI. htmlcontrols. htmlbutton ();
This. _ control3 = _ CTRL;
System. Web. UI. iparseraccessor _ parser = (system. Web. UI. iparseraccessor) (_ CTRL ));
_ Parser. addparsedsubobject (new system. Web. UI. literalcontrol ("OK "));
_ Ctrl. serverclick + = new system. eventhandler (this. onnamesubmit );
Return _ CTRL;
}
Private system. Web. UI. Control _ buildcontrol _ control2 (){
System. Web. UI. htmlcontrols. htmlform _ CTRL;
_ CTRL = new system. Web. UI. htmlcontrols. htmlform ();
This. _ control2 = _ CTRL;
System. Web. UI. iparseraccessor _ parser = (system. Web. UI. iparseraccessor) (_ CTRL ));
_ Parser. addparsedsubobject (new system. web. UI. literalcontrol ("\ r \ n
This. _ buildcontrolnotelabel ();
_ Parser. addparsedsubobject (this. notelabel );
_ Parser. addparsedsubobject (new system. Web. UI. literalcontrol ("\ r \ n "));
This. _ buildcontrolf_name ();
_ Parser. addparsedsubobject (this. f_name );
_ Parser. addparsedsubobject (new system. Web. UI. literalcontrol ("\ r \ n "));
This. _ buildcontrol _ control3 ();
_ Parser. addparsedsubobject (this. _ control3 );
_ Parser. addparsedsubobject (new system. Web. UI. literalcontrol ("\ r \ n "));
Return _ CTRL;
}
Private void _ buildcontroltree (system. Web. UI. Control _ CTRL ){
System. Web. UI. iparseraccessor _ parser = (system. Web. UI. iparseraccessor) (_ CTRL ));
_ Parser. addparsedsubobject (new system. web. UI. literalcontrol ("\ r \ n <HTML> \ r \ n <body> \ r \ n "));
This. _ buildcontrol _ control2 ();
_ Parser. addparsedsubobject (this. _ control2 );
_ Parser. addparsedsubobject (new system. Web. UI. literalcontrol ("\ r \ n </body> \ r \ n
}
Protected override void frameworkinitialize (){
This. _ buildcontroltree (this );
This. filedependencies = ASP. ex2_aspx. _ filedependencies;
This. enableviewstatemac = true;
}
Public override int gettypehashcode (){
Return-11574299;
}
}
}
/* Source File: ex2_aspx.cs ended */
I believe that after analyzing this file, you will have a better understanding of the operating principles of ASP. NET (it is irrelevant to this article and will not be detailed in this article ).
In the command line status, use the following compilation command to compile ex2_aspx.cs into ex2.dll and store it in the bin directory.
T: Library/out: bin \ ex2.dll ex2_aspx.cs
Add aspx-> DLL ing to the configuration file web. config, that is, add the following line to httphandlers in the system. Web Section:
<Add verb = "*" Path = "dlltest2.aspx" type = "ASP. ex2_aspx, ex2"/>
Now, when the browser accesses http: // localhost/dlltest/dlltest2.aspx, it is like accessing ex2.aspx. Of course, even if ex2.aspx does not exist or has been updated, the page access will not be affected unless bin \ ex2.dll is regenerated.
4. Compile the aspx file of codebehind into a DLL
For compiling the aspx file of codebehind into a DLL, the principle of converting the aspx file into a Cs source file is the same as above. It also sets a "code trap" first ", then, sort out the "Complete Compilation source" and save it as the Cs source file. The difference is the step for compiling and translating the SDK into dll: (for ease of description, assume that the interface file is ex3.aspx, The codebehind file is ex3.aspx. CS, and the "Complete Compilation source" of ex3.aspx is saved as ex3_aspx.cs)
Step 1: first use the following command to compile ex3.aspx. cs into bin \ ex3.aspx. cs. dll
T: Library/out: bin \ ex3.aspx. cs. dll ex3.aspx. CS
Step 2: Use the following command to compile ex3_aspx.cs into bin \ ex3.dll
T: Library/R: bin \ ex3.aspx. cs. dll/out: bin \ ex3.dll ex3_aspx.cs
Then add the aspx-> DLL ing in the configuration file web. config, that is, add the following line in the httphandlers section of system. Web:
<Add verb = "*" Path = "dlltest3.aspx" type = "ASP. ex3_aspx, EX3"/>
Open your browser and access http: // localhost/dlltest/dlltest3.aspx.
5. Tips
When setting the "trap" to convert the aspx file to the Cs source file, the "complete compilation source" is usually stored in notepad, vs.net, or other Asp.net development environments using the copy and paste methods, and save it as the Cs source file.
Sort out the row number information entered by paste and the "# Line" Compilation command. If you manually delete this information, it will be too troublesome. Even a simple file such as ex2.aspx will generate about 270 rows of "Complete Compilation source ".
One of my tips is to use the replacement method in notepad to quickly sort out. Replace all rows with "/*", and replace all ":", use "// # Line" to replace all "# Line". After replacement, comment out the "Code trap, comment out all the statements for setting "dependency Files" in the main class constructor, so that the sorting is complete.