{infiniteZest}
// Articles. Tutorials. Utilities.
Home  |   Search  |   Login  
Categories Skip Navigation Links
New / All
AJAX
Apple
ASP.NET
.NET
Git
Google / Android
Python / IronPython
Miscellaneous
SQL Server
IronPython and ASP.NET installation and basics
Summary
This is my first look at installing IronPython for ASP.NET and playing with it a little bit.
 
Table of Contents

Installation

Simple Web site

Figure 1. IronPython Support for ASP.NET web sites

Personal Web Site Starter Kit with IronPython

Figure 2. An IronPython solution for personal web site

Some IronPython example code

Code Listing 1. The code from Default.aspx.py

Code Listing 2. IronPython code from Global.py

 

Installation

I downloaded the ASP.NET Futures relaease (July 2007) from the following location:

http://www.asp.net/downloads/futures/

You can find more information about IronPython for ASP.NET at this location:

http://www.asp.net/ironpython/

Along with other features, this release also contains the support for two dynamic languages from ASP.NET. These are IronPython and ManagedJScript. The installation is as simple as downloading the ASPNETFutures.msi and executing it. It installs these new features to Visual Studio.

Simple Web site

Obviously the purpose of ASP.NET is to create web-related projects. As before, in order to create a new web site, you will use File-New Web Site menu item. As shown in Figure 1, this menuitem brings up the New Web Site dialog box. If you notice the Language dropdown (at the bottom of the dialog box), you now have four options for which language to use in the code-behind of the aspx pages: Visual Basic, Visual C#, IronPython, and ManagedJScript. In the earlier versions, there was support for just two languages: Visual Basic and Visual C#.

Once you chose IronPython as the language, you will also notice now that all the icons for the Visual Studio installed templates now have Py in them -- indicating that they are Python (or IronPython) based projects.

Figure 1. IronPython Support for ASP.NET web sites

Figure 1. IronPython Support for ASP.NET web sites

As you can see from Figure 1, the support for Python (i.e. IronPython) as the code-behind language is full-fledged/comprehensive from the list of Visual Studio installed templates. You see the same templates (ASP.NET Futures AJAX Web Site, ASP.NET Web Site, ASP.NET Futures Web Site, Dynamic Data Web Site, Personal Web Site Starter Kit, and Empty Web Site) as with the original supported languages (C# and VB).

Personal Web Site Starter Kit with IronPython

To play with some IronPython code, I chose the Personal Web Site Starter Kit as the template for my web site. As you can see in Figure 2, a default personal web site has been created for me. In this case, all the code files are in Python and have the .py extension. The code-behind file for Default.aspx is in Default.aspx.py. And the code-behind for Details.aspx is in Details.aspx.py and so on. The global code is in Global.py. In C#, these files would have been named Default.aspx.cs, Details.aspx.cs, etc. Similarly in VB.Net, these files would have been named Default.aspx.vb, Details.aspx.vb, etc.

Figure 2. An IronPython solution for personal web site

Figure 2. An IronPython solution for personal web site

Some IronPython example code

The Listing 1 shows the IronPython code-behind file Default.aspx page. This has the Page_Load method that you might have seen a thousand times before in the C# or VB code-behind files.

Code Listing 1. The code from Default.aspx.py

from System import Random
import PhotoManager

def Page_Load(sender, e):
    photos = PhotoManager.GetPhotosFromRandomAlbum()

    FormView1.DataSource = photos
    FormView1.PageIndex = Random().Next(photos.Count)
    FormView1.DataBind()

If you have not seen a lot of Python code before, following are some notes and comparisons from a C# programmer’s point of view:

- In C#, the code lines end with a semi-colon (;). In Python/IronPython, newline signifies the end of line.

- In IronPython, the functions start with keyword def.

- In C#, the function signature contains the type of return values (like int, void, string, etc.). In IronPython, there is no need for specifying the type of the return value from a function.

- In C#, you use the visibility keywords like public, private, and protected before the function name. In IronPython, there are no such keywords.

- In C#, you specify the types for the parameters. For example, the sender and e parameters for the Page_Load would have been written like (object sender, EventArgs e). In IronPython, the type names object and EventArgs are not needed.

- In C#, blocks (e.g. method body) is enclosed inside curly braces {}. In IronPython, the same effect is achieved via TABs. See all the lines under def are moved one TAB to the right. That is not done for programmer’s sake (prettiness, formatting, etc.); but it is required in IronPython.

- Unlike in C#, there is no need to declare the variables in IronPython. As you can see the ’photos’ variable is not declared in the above listing. IronPython understands the type of that variable from the value returned from PhotoManager.GetPhotosFromRandomAlbum().

Code Listing 2. IronPython code from Global.py

from System import *
from System.Web import *
from System.Web.Security import *

def Application_Start():
    SiteMap.SiteMapResolve += AppendQueryString
    if not Roles.RoleExists(’Administrators’): Roles.CreateRole(’Administrators’)
    if not Roles.RoleExists(’Friends’): Roles.CreateRole(’Friends’)

def AppendQueryString(sender, e):
    node = SiteMap.CurrentNode
    if node:
        node = node.Clone(True)
        qs = e.Context.Request.QUERY_STRING
        if qs:
            node.Url += ’?’ + qs
            if node.ParentNode:
                node.ParentNode.Url += ’?’ + qs
    return node

- We can see the Global.py file in Listing 2. You can see that there two functions in this file -- Application_Start() and AppendQueryString(). You can see these methods are visually separated into two blocks with the help of space in front of the lines belonging to these methods/functions. You can also see three nested if blocks in AppendQueryString() -- the space in front of these lines is required in IronPython.

For C#/C++/Java programmer, this syntax of IronPython looks a little bit weird at first. But after a while, you can see that this is much more readable than the code in those languages.

Bookmark and Share This

More Articles With Similar Tags
icon-ironpython-console.jpg
These are my notes on getting and setting up and playing with IronPython version 1.x and 2.x.
Top Tags in Related Articles
aspnet dynamic language ironpython
About  Contact  Privacy Policy  Site Map