Compile ASPX files into a DLL file

xiaoxiao2021-03-06  113

Compile the ASPX file into a DLL file No. 148 Jiefang North Road, Zhejiang Province, Jiejun 2002-11-12 14:39:19

Preface ASP.NET is not an Asp's simple upgrade, but an important part of the Microsoft .NET plan, which relies on .NET's multilingual and powerful library support, introduced the server HTML control and web control, automatic processing control The client interacts with the server provides developers with interface programming similar to Windows, providing a good programming interface for developing large network application functions, and can greatly improve developers' work efficiency. 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 An Introduction: 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 setting: Create a virtual directory DLLTEST (the real path is w: / wwwroot / dlltest) and set it to the application, and establish 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 Profile --Web.config creates a web.config file in the dlltest directory, the contents of the file are as follows: < / Configuration> Command window (DOS window) Opens the command window and uses the CD command to make the current directory to W: / wwwroot / dlltest.

First, the mapping of the ASPX to DLL first let us take a look at how the ASPX file is processed by ASP.NET: when an HTTP request (such as "http: //webserver/webapp/webpage.aspx") from customers When the end is sent to the IIS server, IIS captures and analyzes this request. When it analyzes this request is an ASPX page, immediately call the ASP.NET running environment (ASPNET_WP.EXE) as the "/webapp/webpage.aspx", After the ASP.NET environment is started, check "/webapp/webpage.aspx" does not exist, if there is no existence, return to the client back to the HTTP 404 (File Not Found) error, otherwise look up the corresponding DLL in the temporary directory of ASP.NET. File, if there is no existence or the DLL is "old" than the ASPX source file, call the CSC compiler (if the ASPX server scripting language is VB or JScript, call the corresponding VBC compiler, JSC compiler) to compile the ASPX file Being a DLL, then ASP.NET calls the DLL to handle the specific customer request and return to 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 ASP.NET application configuration file Web.config, the syntax is as follows: ASPX file: Need to be" routing "virtual name, the extension must be ASPX, otherwise IIS will process the file before the ASP.NET running environment. 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 of a ASP.NET application is as follows: This profile tells the ASP.NET to call the INDEX.ASPX file for the client request this application. The BBS.DLL under the application bin directory and automatically loads the BBS.IndexPage class. Second, the development of the DLL that can handle the HTML page should point out that not all application sets DLL can implement HTTP request / response mode.

Let's take a look at Microsoft ASP.NET Quick Start Tutorial (http://chs.gotdotnet.com/quickstart/aspplus/) About "HTTP Processor and Factory" description: ASP.NET provides low-level request / response API Make 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 {/ * class 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: IHTTPHANDAL {/ * 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. In this example, return a simple HTML page to the client * / public void processRequest (httpresponse res = context.respResponse; 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 an ex1.dll and store it in a bin directory.

CSC / T: library /out :bin/ex1.dll ex1.cs Add ASPX-> DLL mapping in the configuration file web.config, after adding, web.config should be like this: Now when the browser accesses http: //localhost/dllteest/dlltest1.aspx, actually calls the ProcessRequest method of the Dlltest.ex1Page class in EX1.DLL. You should see a simple page in the browse. Third, compiling a single ASPX file into a DLL from the "意意" described by the previous Microsoft, Microsoft does not support the developer directly 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 to 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 obviously does not conform to the C # language specification, so the CSC compiler is unable to The ASPX source file is compiled. 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 #"%>