将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!" OnClick="ButtonClick" 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;
__control2: System.Web.UI.HtmlControls.HtmlForm;
Message: System.Web.UI.WebControls.Label;
procedure ButtonClick(Sender: System.Object; E: EventArgs);
public
constructor Create;
function get_AutoHandlers: Integer; override;
function get_ApplicationInstance: System.Web.HttpApplication; virtual;
function get_TemplateSourceDirectory: System.String; override;
procedure set_AutoHandlers(Value: Integer); override;
protected
property AutoHandlers: Integer read get_AutoHandlers write set_AutoHandlers;
property ApplicationInstance: System.Web.HttpApplication read get_ApplicationInstance;
public
property TemplateSourceDirectory: System.String read get_TemplateSourceDirectory;
private
function __BuildControlEdit1: System.Web.UI.Control;
function __BuildControl__control3: System.Web.UI.Control;
function __BuildControl__control2: System.Web.UI.Control;
function __BuildControlMessage: System.Web.UI.Control;
procedure __BuildControlTree(__ctrl: System.Web.UI.Control);
protected
procedure FrameworkInitialize; override;
public
function GetTypeHashCode: Integer; override;
end;
implementation
procedure editdemo_aspx.ButtonClick(Sender: System.Object; E: EventArgs);
begin
Message.Text := Edit1.Text;
end;
constructor editdemo_aspx.Create;
var
dependencies: System.Collections.ArrayList;
begin
inherited Create;
if (ASP.editdemo_aspx___intialized = False) then
begin
dependencies := System.Collections.ArrayList.Create;
dependencies.Add('d:\vslive\editdemo.aspx');
ASP.editdemo_aspx___fileDependencies := dependencies;
ASP.editdemo_aspx___intialized := True;
end;
Self.Server.ScriptTimeout := 30000000;
end;
function editdemo_aspx.get_AutoHandlers: Integer;
begin
Result := ASP.editdemo_aspx___autoHandlers;
end;
function editdemo_aspx.get_ApplicationInstance: System.Web.HttpApplication;
begin
Result := Self.Context.ApplicationInstance as System.Web.HttpApplication;
end;
function editdemo_aspx.get_TemplateSourceDirectory: System.String;
begin
Result := '/vslive';
end;
procedure editdemo_aspx.set_AutoHandlers(Value: Integer);
begin
ASP.editdemo_aspx___autoHandlers := Value;
end;
function editdemo_aspx.__BuildControlEdit1: System.Web.UI.Control;
var
__ctrl: System.Web.UI.WebControls.TextBox;
begin
__ctrl := System.Web.UI.WebControls.TextBox.Create;
Self.Edit1 := __ctrl;
__ctrl.ID := 'Edit1';
__ctrl.Width := System.Web.UI.WebControls.Unit.Parse('300px', System.Globalization.CultureInfo.InvariantCulture);
Result := __ctrl;
end;
function editdemo_aspx.__BuildControl__control3: System.Web.UI.Control;
var
__ctrl: System.Web.UI.WebControls.Button;
begin
__ctrl := System.Web.UI.WebControls.Button.Create;
Self._