第一个 IronPython 的 ASP.NET 程序

今天试验了在 Visual Studio 中集成使用 IronPython,记录如下。

首先,下载一个 IronPython 1.0 的 binary,解压后,将目录路径 配置到环境变量 Path 中。
然后下载最新的 Visual Studio SDK. 我下载的是 August 2006 的测试版。
(下载地址:http://affiliate.vsipmembers.com/affiliate/downloadfiles.aspx)

安装完 SDK 后,可以在 Visual Studio 中使用 IronPython 了。打开
C:\Program Files\Visual Studio 2005 SDK\2006.04\VisualStudioIntegration\Samples\IronPythonIntegration
的项目,按 F5 运行,这时会启动一个新的 Visual Studio 实例,其中可以创建 IronPython 项目了。
我试了几下,发现现在还非常不稳定,经常会出现异常。

这个启动的 Visual Studio 实例中支持对 IronPython 的语法高亮,和 Intellisense 支持。
另外环境中还带一个 IronPython 控制台窗口(View-Other Windows 菜单中打开),可以用于方便的试验各种对象和方法:

ipy2.JPG

接下来创建一个 ASP.NET 项目,在 .aspx 文件里的写法是几乎没有区别的,但是不知道为什么一切换到 designer 就会抛出异常

没办法,只好手写了:

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.py" Inherits="WebApplication1._Default" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
    
<title>Untitled Page</title>
</head>
<body>
    
<form id="form1" runat="server">
    
<div>
        
<asp:TextBox ID="txtName" runat="server" />
        
<asp:Button ID="btnTest" Text="OK" runat="server" OnClick="btnTest_Click" />
    
</div>
    
</form>
</body>
</html>

写完这些后,在后台代码里添加了一个 btnTest 按钮的处理方法如下:

import System
from System.Web import *
from System.Web.UI import *
from System.Web.UI.WebControls import *
from System.Web.UI.WebControls.WebParts import *
from System.Web.UI.HtmlControls import *
from clr import *

class WebApplication1: #namespace
    class _Default(System.Web.UI.Page):
        @accepts(Self())
        @returns(None)
        
def Page_Load(self):
            
pass
        
        
def btnTest_Click(self, sender, args):
            self.txtName.Text 
= "Hello"

            

语法看起来的确比 C# 的要简单一些

真的很期待下一个 Visual Studio 补丁出来,那样就可以在 ASP.NET 的实际开发中体验 IronPython 了。

posted on 2006-09-15 18:17  NeilChen  阅读(3378)  评论(4编辑  收藏  举报

导航