Registered member, create your web development database, preface
ASP.NET is not a simple upgrade of ASP, but an important part of the Microsoft .NET plan. Relying on the multi -language and powerful library support of .NET. The interaction between the client and the server provides developers with an interface similar to the window programming of Windows, provides a good programming interface for the development of large network application functions, and can greatly improve the work efficiency of developers.
However, the "one -time conversion, two compilations" process makes the ASPX file appear slightly inadequate when the first execution (or the first operation after update), especially in the application environment of a large number of ASPX and codehind code files, compile the ASPX file compile It is published after DLL (in the .NET, known as the application set), eliminating the time of "one conversion, one compilation" and the CPU usage rate, which will greatly improve the overall performance of Web services. Of course, after being compiled into DLL, the confidentiality of the source code has also improved to a certain extent.
This article introduces the basic processing process of ASP.NET and an analysis of a secret discovery. It introduces how to establish ASPX to DLL mapping in ASP.NET, how to develop a DLL that can process HTTP request/response, and how to set " "Trap", compiles the available ASPX files with CodeBehind's ASPX file into DLL process. At the end of the article, a tips for the actual operation process are also introduced.
Since this article involves the concepts such as ASP.NET applications, command line compilation, web.config configuration files, etc., in order to enable readers to better understand the content of this article, and to make this article look no longer accumulated, first of all the systems corresponding to this article, this article is corresponding to the corresponding system of this article. Introduction to the environment:
System environment:
Win2000 (SP3) + IIS5 + .NET Framework 1.0 (Chinese version).
Server name:
Since the examples of this article are tested on this machine, the server name is Localhost.
IIS settings:
Establish a virtual directory DLLTEST (the real path is w:/wwwroot/dlltest), and set it as an application to create a BIN directory under dlltest. All source files will be placed in the DLLTEST directory, and all DLL files will be placed in the DLLTEST/BIN directory.
ASP.NET application configuration file --web.config
Create a web.config file in the dlltest directory. At the beginning, 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 use the CD command to make the current directory as w:/wwwroot/dlltest.
1. Establish a mapping from ASPX to DLL
First of all, let's see how the ASPX file is processed by ASP.NET:
When a http request (such as "http://webserver/webapp/webpage.aspx") is sent from the client to the IIS server, IIS captures and analyzes the request, and when it analyzes this request as an ASPX page, immediately use the ASPX page to immediately use the ASPX page. "/Webapp/Webpage.aspx" is the parameter call the ASP.NET operating environment (ASPNET_WP.EXE), and after the ASP.NET environment is started, check whether the "/Webapp/webpaage.aspx" exists. If it does not exist, return to the client and return to HTTP 404 (File Not Found) error, otherwise find the corresponding DLL file in the temporary directory of ASP.NET, if it does not exist or the DLL is "old" than the ASPX source file, call the CSC compiler (if the ASPX service script of the service side script of the ASPX The language is VB or JScript, then calls the corresponding VBC compiler, JSC compiler) to compile the ASPX file into DLL, and then ASP.NET calls the DLL to handle specific customer requests and return to the server response.
It can be seen from this processing process. Generally, the ASP.NET operating environment will automatically identify, check, and update the DLL corresponding to ASPX. So is there any other way to force the processing of "route" to a ASPX file to a compiled DLL? The method is to add ASPX to DLL mapping items in the httphandlers section of the System.Web section of the System.Web section in the ASP.NET application.
<dd Verb =* PATH = ASPX file name type = class name, dll file/>
ASPX file: The virtual name of the "routing" is required, and the extension must be aspx, otherwise IIS will process the file first at the ASP.NET running environment.
DLL file: DLL file (application set) name, no need to enter ".dll". ASP.NET first searches the assembly DLL in the application of the application for the application of the application, and then searches the assembly DLL in the system set cache cache.
Class name: Since one DLL may have multiple names or multiple classes, which class must be automatically loaded when DLL calls.
For example, the web.config file of a certain ASP.NET application is as follows:
<? XML Version = 1.0?>
<configuration>
<system.web>
<httphandler>
<dd Verb =* Path = Index.aspx Type = BBS.Indexpage, BBS />
</httphandler>
</ssystem.web>
</configuration>
The configuration file tells ASP.NET that when the client requests the index.aspx file of this application, directly calls the bbs.dll in the application bin directory, and automatically loads the bbs.indexpage class.
2. Development can handle the DLL of the HTML page
It should be pointed out that not all applications DLL can implement HTTP request/response mode. Let's take a look at the description of "http processing program and factory" in the Microsoft ASP.NET fast entry tutorial (http://chs.gotdotnet.com/quickstart/aspplus/):
ASP.NET provides low -level requests/response APIs, enabling developers to use the .NET framework class to provide services for HTTP requests that were introduced. To this end, developers need to create a class that supports System.web.ihttphandler interface and implement the processRequest () method. When processing the HTTP request does not require the service provided by the high -level page framework, the processing program is usually useful. Common uses of processing programs include screenters and applications similar to CGI, especially those applications that return binary data.
Each request from the HTTP request received by ASP.NET is finally handled by a specific instance of the class that implements IHTTPHANDLER. IHTTPHANDLERFACTORY provides a structure that processes the actual analysis of the IHTTPHANDLER instance URL request. 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 analysis and activation solutions.
It can be seen from this text that when the ASPX page does not involve the advanced interface technology provided by the .NET framework (such as data cache, state keeping, Web window control reference, etc.) , Especially when returning binary data (such as pictures, sounds, etc.) to the client, you can use a .cs application file (using C#language here, if you use VB or JScript, ...), and the application The program must have a class to implement System.web.ihttphandler interface and implement the processRequest () method. A simple example is as follows:
/* Source file: EX1.CS starts*/
using system.web;
namespace dlltenst
{{
/*
The class must implement the IHTTPHANDLER interface. If the program will access the session status, the IrequiressessionState interface must be implemented (the marker interface that does not include any method).
*/
Public Class Ex1Page: IHTTPHANDLER
{{
/*
Isreusable attribute tells the .NET framework that this program can be used by multiple threads at the same time.
TRUE corresponds; whether the false corresponds.
*/
Public BOOL Isreusable
{{
get {Return True;}
}
/*
Implement the processRequest method and return the response data to the client.
In this example, return a simple HTML page to the client
*/
Public Void ProcessRequest (httpcontext context)
{{
httpresponse res = context.Response;
res.Write (<html> <body>);
res.Write (<h1> dlltest -ex1 (example 1) </h1> <hr>);
Res.Write (this page is handled directly by DLL);
res.Write (</html> </body>);
}
}
}
/* Source file: EX1.CS end*/
In the command line state, the following compile commands compile ex1.cs into EX1.dll and store it in the BIN directory.
CSC /T: library /Oxin/ex1.dll ex1.cs
Add ASPX-> DLL mapping to the configuration file web.config. After adding, web.config should be like this:
<? XML Version = 1.0?>
<configuration>
<system.web>
<httphandler>
<DED VERB =* PATH = DLLTEST1.ASPX Type = Dlltest.ex1page, EX1 />
</httphandler>
</ssystem.web>
</configuration>
Now when the browser visits http://localHost/dllTest/dlltest1.aspx, it is actually the processRessrequest method of dlltest.ex1page class in ex1.dll. You should see a simple page in the browse.
Third, compile a single ASPX file into DLL
Judging from the "meaning outside of Microsoft" publicly described in the previous section, Microsoft does not support developers to compile ASPX files directly into DLL. However, ASP.NET Advanced Interface Technology (server HTML control, web control, etc.) needs to be displayed through the ASPX file. If you abandon the advanced characteristics of ASPX for the operation efficiency of DLL, you obviously have to lose your loss. of.
Now calm down to analyze:
The CSC compiler is just a C#language compiler. It can only compile files that meet the C#language specifications, and the format of the ASPX file obviously does not comply with the C#language specification, so the CSC compiler cannot compile the ASPX source file.
Therefore, in order to compile the ASPX file into a DLL file, the ASPX file must be converted into a CS source file that can be recognized by the CSC compiler. So what tools are used to convert? Although I believe that this tool must be hidden in .NT Framework, I checked a large number of ASP.NET and .NET public documents and reference manuals. After the information, the relevant information was still not found.
Oh, there is no way to the world, and an accidental opportunity still makes me discover this secret.
Take a look at the source file EX2.ASPX:
/* Source file: ex2.aspx Start*/
< %@ page language = c# %>
<script runat = server>
/*
You read it right, the next line is "ABCDEFG". It is this line that gives me a chance to write this article^_^;
In the text, I call this line "code trap"
*/
abcdefg // code trap
void page_load (Object SRC, EventArgs ARGS)
{{
if (! ispostback) notLabel.text = Please enter your name:;
}
void onNamesubmit (Object SRC, EventArgs ARGS)
{{
string name = f_name.value;
notLabel.text = (name ==)? The name cannot be empty: name +, hello. Welcome! ;;
}
</script>
<html>
<body>
<FORM RUNAT = Server>
<h1> dlltest -ex2 (example 2) </h1>
<hr>
<asp: label runat = server id = notLabel 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 end*/
If the "code trap" comments or deletes, then EX2.ASPX is a simple ASP.NET file. Browse this page with IE can find that it can work normally.
Now let's open the "trap" and see what the ASP.NET has returned?
Returned a "compilation error" page, and the report source files cannot be compiled. What is interesting to be interested is the hyperlink called "Show the complete compilation source" at the bottom of the page. Click on some links to see this CS source file converted from EX2.ASPX ("complete compilation Source ") complete content. Remove this part of the "complete compilation source" and remove the previous line number information and some other compilation switches (mainly #Line compile commands) and close the cute "code trap" It delete also), and after finishing, save it as ex2_aspx.cs:
/* Source file: ex2_aspx.cs Start*/
/*
From the description below, it can be seen that there is indeed an uninterrupted tool to complete the converting ASPX file into a CS source file
*/
// --------------------------------------------------------------------------------------------------------------------------------------------------------------------- ----------------------------------------------------------------------------
// <emutogenerate>
// This code was generated by a too.
// Runtime version: 1.0.3705.0
//
// CHINGES to this file may care incorrect behavior and will be lost if
// The code is regenerated.
// </autogenerate>
// --------------------------------------------------------------------------------------------------------------------------------------------------------------------- ----------------------------------------------------------------------------
/*
Strangely: Naming space is actually ASP instead of ASPX
It is recommended to change the name to the name suitable for applications to prevent naming conflict, such as for this article, you can change to dlltest
I didn’t change here to let everyone see its original appearance
*/
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, for this article, you can change to ex2Page
2. Pay attention to its base class. System.web.ui.page implements the IHTTPHANDLER interface. Since I want to access session, IrequiresSenseState interface is also implemented.
*/
public class ex2_aspx: system.web.ui.page, system.Web.SessionState.irequiresSenerationState {
Private static int __autohandler;
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) notLabel.text = Please enter your name:;
}
void onNamesubmit (Object SRC, EventArgs ARGS)
{{
string name = f_name.value;
notLabel.text = (name ==)? The name cannot be empty: name +, hello. Welcome! ;;
}
/* Construction function*/
public ex2_aspx () {
System.Collections.arrayList Dependencies;
if (asp.ex2_aspx .__ intialize == false) {{
dependencies = new system.Collections.arrayList ();
/*
The following lines should be annotated so that DLL becomes an independent independent file
Prevent the new and old of its "dependence" 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 Autohandler {
get {
Return asp.ex2_aspx .__ Autohandler;
}
set {
asp.ex2_aspx ._ Autohandlers = Value;
}
}
ProteCted System.web.httpage 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.notlabel = __ctrl;
__Ctrl.id = Notelabel;
(System.web.ui.iattributeaccessor) (__ Ctrl). Setattribute (style, color: red; font-weight: black);
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
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 <h1> dlltest -ex2 (example 2) </h1>/r/n <hr>/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/r/n <html>/r/n <body>/r/n));
this .__ buildControl__Control2 ();
__Parser.addparsEdsubobject (this .___ Control2);
__Parser.addparsEdsubobject (new system.web.ui.literalControl
}
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 end*/
I believe that after analyzing this file, you will have a further understanding of the principle of ASP.NET operation (have nothing to do with this article, unreasonable).
In the command line state, the following compile commands compile ex2_aspx.cs into ex2.dll and store it in the BIN directory.
CSC /T: library /Oxin/ex2.dll ex2_aspx.cs
Add ASPX-> DLL mapping to the configuration file web.config, that is, add the following lines in httphandlers in the System.Web section:
<DERB =* PATH = DLLTEST2.ASPX Type = ASP.EX2_ASPX, EX2 />
Now when the browser visits http://localhost/dllTest/dlltest2.aspx, it is just like accessing ex2.aspx. Of course, even if EX2.ASPX does not exist, or it has been updated, it will not have any impact on page access, unless Bin/EX2.dll is re -generated.
Fourth, compile the CodeBehind's ASPX file into DLL
For compiling the CodeBehind's ASPX file into 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, and then properly organizes the "complete compilation source" to save it as a CS source file. The difference is the steps when compiled into DLL: (for convenience of narrative, assuming that the interface file is ex3.ASPX, the codebehind file is ex3.aspx.cs, and the "complete compilation source" of EX3.ASPX is stored as ex3_aspx.cs))
Step 1: First use the following command to compile ex3.aspx.cs into bin/ex3.aspx.cs.dll
CSC /T: library /Oxin/ex3.aspx.cs.dll ex3.aspx.cs
Step 2: Use the following command to compile ex3_aspx.cs into bin/ex3.dll
CSC /T: library /r:bin/ex3.aspx.cs.dll /out:bin/ex3.dll ex3_aspx.cs
Then add ASPX-> DLL mapping to the configuration file web.config, that is, add the following lines in httphandlers in the System.web section:
<add Verb =* PATH = DLLTEST3.ASPX Type = ASP.EX3_ASPX, EX3 />
Now open the browser and visit http://localhost/dllTest/dllTest3.aspx.
Five, a little trick
When setting the "trap" to convert the ASPX file into a CS source file, generally use the Copy and Paste method to save the "complete compilation source" in the notepad or vs.net or other ASP.NET development environment, and then save it after finishing the preservation For CS source files.
Organizing is to remove the line number information of the Paste and the "#Line" compilation instruction. If this information is deleted manually, it will be too troublesome. Even a simple file such as ex2.aspx will produce about 270 lines of "complete compilation source".
One of the tricks I use is: In the notebook, use the replacement method to quickly organize. Use/* line to replace all the lines, use:*/to replace all:, use // #line lines to replace all #Line, after replacement is completed, annotate the "code trap", set the main class structure function settings, set the settings of the main constructor function The sentences of "dependency files" are annotated, so even if the finishing is complete.