Asp.NET with IronPython Integration

Cuma, 16 Ekim 2009 14:09 by ikivanc
One of the IronPython development area is Asp.NET. In this article we will see Visual Studio 2008 Asp.Net integration. 

Then download the Asp.NET IronPython integration for visual Studio 2008 installer here.



Asp.NET with IronPython is really rocks! This combination gives us time/performance advantage. Especially in small scripts for events, python code is too short and powerful! So we can prototype projects quickly.

Because of Dynamic Language, it will work server-side on client’s machine and it works dynamicly. With this feature we can connect websites faster. 

Let’s check out, files in a project from Solution Explorer.
 
    IRONPYTHON                                                             C#
 
After creating a new web project, “bin” folder, "default.aspx" page, "Global.py" file and "Web.config" file are created by Visual Studio.

"bin" Folder
It contains our website DLLs for running IronPython on the web. 

Default.aspx
It’s our default homepage for web project. It work with code-behind file, Default.aspx.py which contains all python code for page.

Global.py
This file contains some situations about web application for some cases.

def Application_Start():
    ' Code that runs on application startup' 
    pass
 
def Application_End():
    ' Code that runs on application shutdown'
    pass 
 
def Application_Error(app, e): 
    ' Code that runs when an unhandled error occurs'
    pass 
 
def Application_BeginRequest(app, e): 
    ' Code that runs at the beginning of each request'
    pass 
 
def Application_EndRequest(app, e):
    ' Code that runs at the end of each request'
    pass 

web.config
Some Asp.NET web project configurations included in it. 

What is the differences between “C# Asp.Net” and “IronPython Asp.Net”?
First differences In Ipy Project, it contains “bin” folder which contains DLL libraries for IronPython and global.py file.

The most helpful advantage of IronPython is easy and short code. Usually people compare C# and IronPython with their code line.

Let’s check out the example below
The web page contains label and a button. First page initialize button text as “Click” and Page title as “IronPython Example” on Page Load. After clicking button, label text will be “Clicked” on Button1 Click. 

C# 
public partial class _Default : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            Button1.Text = "Click!";
            this.Page.Title = "C# Example";
        }
 
        protected void Button1_Click(object sender, EventArgs e)
        {
            Label1.Text = "Clicked";
        }
    } 

IronPython 
def Page_Load(sender, e):
    Button1.Text = 'Click'
    Page.Title = 'IronPython Example'
 
def Button1_Click(sender, e):
    Label1.Text = 'Clicked' 
 
Also in IronPython we don’t have to import standard libraries. Now Programming is really enjoyable! :)

PDF verison of this article >>  5 - IronPython ASP.NET Integration

If you have any questions or discover any errors / typos please let me know ik@ibrahimkivanc.com

All The Best!

Yorumlar