日期:2010-05-26  浏览次数:20398 次

   将Delphi视为脚本语言
  
   支持ASP.net的第一件事是让ASP.NET将Delphi视为脚本语言,让ASP.NET能够为各种ASP文件类型调用Delphi的.NET编译器。
  
   ASP.NET要在IIS虚路径的根目录下寻找Web.config文件。下面是ASP.NET中使用Delphi作脚本语言的web.config配制文件内容:
  
   <configuration>
   <system.web>
   <compilation debug="true">
   <assemblies>
   <add assembly="DelphiProvider" />
   </assemblies>
   <compilers>
   <compiler language="Delphi" extension=".pas"
   type="Borland.Delphi.DelphiCodeProvider,DelphiProvider" />
   </compilers>
   </compilation>
   </system.web>
   </configuration>
  
   关于web配制文件的详情请参MSDN:
   http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpguide/html/cpconformatofconfigurationfiles.asp
  
   在我的机器(Win2K Pro)上测试Delphi对ASP.NET的支持。要增加一个虚路径来安放Delphi脚本,我把这个路径名定为"vslive"。
  
   在我的机器配制文件上作了这个小改动后就可以来看ASP.NET是如何将Delphi视为脚本语言的。
  
   在ASP.NET中使用Delphi代码
   首先编写一个简单应用来确认我们的Delphi支持配制是正确的。下面是editdemo.aspx文件的代码,有一个文本输入框,一个按钮。按钮事件触发显示输入框内容。
  
   <html>
   <script language="Delphi" runat="server">
   procedure ButtonClick(Sender: System.Object; E: EventArgs);
   begin
   Message.Text := Edit1.Text;
   end;
   </script>
   <body>
   <form runat="server">
   <asp:textbox id="Edit1" runat="server"/>
   <asp:button text="Click Me!" runat="server"/>
   </form>
   <p><b><asp:label id="Message" runat="server"/></b></p>
   </body>
   </html>
  
   可以看到,ASP按钮对象的OnClick事件触发Delphi的ButtonClick过程,而Delphi程序里却使用了ASP对象label Message和text box Edit1。虽然在Delphi过程中没有定义ASP的二个变量,Delphi代码却能找到它们。这是因为脚本中含有的Delphi服务模块能够产生相应的Delphi代码。
  
   现在可以用本地浏览器来浏览这个网页了。因为我把脚本放在"vslive"虚路径,所以打开:
   http://localhost/vslive/editdemo.aspx.
  
   以下是编译之后形成的Delphi代码,代码产生WebForm:

  //------------------------------------------------------------------------------
  // <autogenerated>
  // 本代码由一个工具生成
  // 运行版本:1.0.3705.209
  //
  // 修改本文件将导致异常行为并丢失生成的代码。
  // </autogenerated>
  //------------------------------------------------------------------------------
  
  unit ASP;
  
  interface
  
  uses System.Collections, System.Collections.Specialized, System.Configuration,
   System.Text, System.Text.RegularExpressions, System.Web, System.Web.Caching,
   System.Web.SessionState, System.Web.Security, System.Web.UI, System.Web.UI.WebControls,
   System.Web.UI.HtmlControls, System.Globalization;
  
  var
   editdemo_aspx___autoHandlers: Integer;
   editdemo_aspx___intialized: Boolean = False;
   editdemo_aspx___fileDependencies: System.Collections.ArrayList;
  type
   editdemo_aspx = class(System.Web.UI.Page, System.Web.SessionState.IRequiresSessionState)
   protected
   Edit1: System.Web.UI.WebControls.TextBox;
   __control3: System.Web.UI.WebControls.Button;
   __contr