Compile ASPX files into a DLL file

xiaoxiao2021-04-10  511

ASP.NET is not a simple upgrade of ASP, but an important part of the Microsoft .NET plan, which relies on .NET's multilingual and powerful class library support, introduced the server HTML control and web control, automatically handles controls. Client and server

Interaction, providing developers with interface-like interface programming, providing a good programming interface for developing large network application functions, and can greatly improve the work efficiency of developers.

However, "One Transfer, Two Compilation" procedures makes the ASPX files in the first execution (or updated first run), especially in an application environment with a large number of ASPX and codebehind, put the ASPX file After the DLL (in .NET, known as the application set), release, saving the time and CPU usage of "one transition, one compilation", and the overall performance of the Web service will have a large increase. Of course, after compiling into a DLL, the confidentiality of the source code has a certain degree.

This article introduces how to establish an ASPX to DLL mapping in ASP.NET through the basic processing flow of ASP.NET, how to establish an ASPX to DLL map, how to develop a DLL that can handle HTTP request / response, and how to set up Trap ", compiling the ready-made single ASPX file with codebehind's ASPX file into a DLL process, and the article is finally introduced to a small skill in the actual operation process.

Since this paper should involve the ASP.NET application, the command line compilation, web.config configuration file, etc. In order to make the reader better understand the content of this paper, in order to make this article, the system will correspond to the system corresponding to this article Environmental introduction:

System environment:

Win2000 (SP3) IIS5 .NET Framework 1.0 (Chinese).

name of server:

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 to the 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, the contents of the file are as follows:

Command window (DOS window)

Open the command window and use the CD command to make the current directory to W: / wwwroot / dlltest.

First, establish an ASPX to DLL mapping

Let us first take a look at how the ASPX file is processed by ASP.NET:

When an HTTP request (such as "http: //webserver/webapp/webpage.aspx" is sent from the client to the IIS server, IIS captures and analyzes this request, when it analyzes this request is an ASPX page, immediately "/Webapp/webpage.aspx" calls the ASP.NET Run environment (ASPNET_WP.EXE) for the parameter, after the ASP.NET environment is started, check "/WebApp/webpage.aspx", if there is no existence, return to the client HTTP 404 (FILE NOT FOUND) Error, otherwise lookup corresponding DLL file in the provisional directory of ASP.NET, if there is no existence or the DLL is "old" than the ASPX source file, then call the CSC compiler (if the ASPX server script The language is VB or JScript, then call the corresponding VBC compiler, the JSC compiler) compiles the ASPX file into a DLL, then the ASP.NET calls the DLL to handle the specific customer request, return the server response. As can be seen from this processing process, in general, the ASP.NET running environment automatically recognizes, checks, updates DLL corresponding to ASPX. So do you have any other way to force the process of processing a ASPX file to a DLL that has been compiled? The method is to add ASPX to DLL mapping items to the Httphandlers section of the System.Web section of the ASP.NET application profile Web.config. The syntax is as follows:

ASPX file: You need to be "routing" virtual name, the extension must be ASPX, otherwise IIS will process the file before the ASP.NET runs.

DLL file: The name of the DLL file (application set) does not have to enter ".dll". ASP.NET first searches for assembly DLL in the application's dedicated / bin directory, then search the assembly DLL in the System Program Cache.

Class name: Since a DLL may have multiple namespaces or multiple classes, you must specify which class is automatically loaded when DLL calls.

For example, a web.config file for a ASP.NET application is as follows:

This profile tells ASP.NET, when the client requests the INDEX.ASPX file of this application, directly calls the BBS.DLL under the application bin directory, and automatically loads the BBS.IndexPage class.

Second, develop DLL that can handle HTML pages

It should be noted that not all application sets DLL can implement HTTP request / response mode. Let's take a look at the Microsoft ASP.NET Quick Start Tutorial (http://chs.gotdotnet.com/quickstart/aspplus/) Description for HTTP Processors and Factory:

ASP.NET provides a low-level request / response API to enable developers to provide services using the .NET Framework class as incoming HTTP requests. To this end, developers need to create classes that support the System.Web.ihttPHandler interface and implement the processRequest () method. The handler is usually useful when processing HTTP requests do not require services provided by a high-level page framework. The common use of the handler includes filters and applications similar to CGI, especially those that return binary data. Each incoming HTTP request received by ASP.NET is finally processed by a specific instance of the class that implements the IHTTPHANDLER. IHTTPHandlerFactory provides the actual parsing 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 parsing and activation.

As can be seen from this text, when the ASPX page does not involve the advanced interface technology provided by the .NET framework (such as data cache, status, web form control reference, etc.), and output to the client is not a complex HTML text. In particular, when binary data (such as pictures, sounds, etc.) is returned to the client, can be used in a .cs application file (this article uses the C # language, if it is used in VB or JScript, ...), The program must have a class that implements the System.Web.ihttPHandler interface and and implements the processRequest () method. A simple example is as follows:

/ * Source file: EX1.CS start * /

Using system.Web;

Namespace DLLTest

{

/ *

Classs must implement the IHTTPHANDLER interface. If the program will access a session status (Session), you must implement the IREQUIRESSESSITIONSTATE interface (not containing tag interfaces for any method).

* /

Public Class Ex1page: IHTTPHANDLER

{

/ *

The isreusable property tells the .NET framework, whether the program can be used simultaneously by multiple threads.

TRUE corresponds to whether false is right.

* /

Public Bool IsReusable

{

Get {return true;}

}

/ *

Implement the ProcessRequest method to return response data to the client.

This example returns a simple HTML page to the client.

* /

Public void processRequest (httpcontext context)

{

HttpResponse res = context.response;

Res.Write (" ");

Res.Write ("

DLLTEST - EX1 (Example 1)
");

Res.Write ("This page is processed directly by DLL");

Res.Write (" );

}

}

}

/ * Source file: EX1.CS end * /

In the command line status, use the following compile command to compile EX1.CS into ex1.dll and store it in a bin directory.

CSC / T: library /out:bin/ex1.dll ex1.cs

Add an ASPX-> DLL mapping in the configuration file web.config. After adding, Web.config should be like this:

Now when the browser accesses http: //localhost/dlltest/dlltest1.aspx, it is actually called the ProcessRequest method of the DLLTEST.EX1PAGE class in EX1.DLL. You should see a simple page in the browse.

Third, compile a single ASPX file into a DLL

From the previous section, Microsoft disclosed, Microsoft is not supported to let developers directly compile the ASPX file into DLL. However, ASP.NET Advanced Interface Technology (server HTML control, web control, etc.) is needed to be displayed through the ASPX file. If the advanced features of ASPX are abandoned for the running efficiency of the DLL, it is obviously not to try. of.

Now calm down and analyze:

The CSC compiler is just a C # language compiler, which can only compile the C # language specification, and the format of the ASPX file clearly does not comply with the C # language specification, so the CSC compiler is unable to compile the ASPX source file.

Therefore, if you want to compile the ASPX file as a DLL file, you must first turn the ASPX file into the CSC compiler to identify the CS source file. So what tool is used to convert? Although I am convinced that this tool must be hidden in .NET Framework, but instead of reviewing a large number of ASP.NETs and .NET's public documents and reference manuals, the information will not be found.

Oh, there is no road to the sky, an accidental opportunity, or let me find this secret.

Take a look at the source file EX2.ASPX:

/ * Source file: EX2.ASPX start * /

<% @ Page language = "c #"%>