Using Dynamic Languages with ASP.NET
This walkthrough provides you with an introduction to dynamic languages for ASP.NET. It guides you through creating a simple page in Microsoft Visual Studio, adding controls, and adding event handlers using dynamic languages.
Tasks illustrated in this walkthrough include:
- Adding controls to the default page.
- Adding event handlers in a separate code file, using dynamic languages.
- Adding a second page with event-handling code in the page.
Prerequisites
In order to complete this walkthrough, you will need:
- Microsoft Visual Studio 2008 or Visual Web Developer 2008 Express Edition.
- A copy of the website included in the ASP.NET Dynamic Language Support download. There is currently no project template, so it is necessary to copy the website in order to start with a blank ASP.NET Dynamic Language website.
This walkthrough assumes that you have a general understanding of working in Visual Web Developer. For an introduction, see Walkthrough: Creating a Basic Page in Visual Web Developer.
Creating a Web Site
In this part of the walkthrough, you will create a Web site with a dynamic language as the default language.
To create a Web site with a default ASP.NET Web page
- Copy the files from the ASP.NET Dynamic Language Support project into an empty directory.
- In Visual Studio (or Visual Web Developer), in the File menu, click Open Web Site. The Open Web Site dialog box is displayed.
- Select the directory in which you copied the files in step 1. Make sure that FileSystem is selected in the left panel of the dialog.
Note: You can use statically compiled languages in the same Web application by creating pages and components in different programming languages.
- Click Open. Visual Studio opens the folder as a website and displays the files in the Solution Explorer.
Adding Controls to the Default Page
In this part of the walkthrough, you will add server controls to the page.
To add controls to the page
- Switch to Design view.
- In the Toolbox, from the Standard group, drag three controls onto the page: a TextBox control, a Button control, and a Label control.
- Put the insertion point above the TextBox control, and then type Enter your name: to create a caption for the text box.
Programming the Button Control
For this walkthrough, you will write code that reads the name that the user enters into the text box and then displays the name in the Label control.
To add a button event handler
- Right-click the page and click View Code to show the separate code file. For example, if you are using IronPython, the file is Default.aspx.py.
The file contains a stub event handler for the Load event of the page.
Note: In IronPython, pass is a placeholder that does nothing.
- Replace the stub event handler with the following code to set the label text when the page is initialized:
IronPython
def Page_Load(sender, e): if not IsPostBack: Label1.Text = "...Your name here..."
- Add the following code to create an event handler for the button's
Clickevent:IronPython
def Button1_Click(sender, e): Label1.Text = Textbox1.Text
In this release, event handlers must be coded and bound manually. You cannot create them by double-clicking a control in Design view or by selecting an event in the Properties window.
Because dynamic languages do not have typed parameters and variables, you do not need to know the type of the event argument object.
Note: In this release, IntelliSense support for dynamically typed variables is limited. You can press CTRL+SPACE to get a list of code elements that are currently in scope.
- Switch to Default.aspx and go to Source view, and then bind the event handler by adding an
OnClickattribute to the Button control markup, as shown in the following example:<form id="form1" runat="server"> <div> Enter your name:<br /> <asp:TextBox ID="TextBox1" runat="server"> </asp:TextBox> <asp:Button ID="Button1" runat="server" Text="Button" OnClick="Button1_Click"/><br /> <br /> <asp:Label ID="Label1" runat="server" Text="Label"> </asp:Label> <br /> </div> </form>
- Press CTRL+F5 to run the page in the browser using the ASP.NET Development Server.
- Enter a name into the text box and click the button. The name you entered is displayed in the Label control. If the name does not appear, check the spelling of the event handler in the
OnClickattribute. - In the browser, optionally view the source of the page you are running.
- Close the browser.
Programming the Button Control
For this walkthrough, you will add dynamic language code in a script block.
To add a default button event handler
- Switch to Source view.
- Add the following code to initialize the label and to create an event handler for the button's Click event.
IronPython
<script runat="server"> def Page_Load(sender, e): if not IsPostBack: Label1.Text = "...Your name here..." def Button1_Click(sender, e): Label1.Text = Textbox1.Text </script>
- In the Button control, bind the event handler by adding the
OnClickattribute, as you did previously in this walkthrough. The following example shows the markup.<asp:Button ID="Button1" runat="server" Text="Button" OnClick="Button1_Click"/><br />
- Press CTRL+F5 to run the page in the browser using the ASP.NET Development Server.
CLR Security in .NET 3.5
Support for “Suite B” crypto algorithms on Vista
Elliptic Curve Diffie Hellman for key exchange (ECDiffieHellmanCng class)
Elliptic Curve Digital Signature Algorithm for signing (ECDsaCng class)
FIPS certified Advanced Encryption Standard (AES) for symmetric encryption (AesCryptoServiceProvider class)
FIPS certified SHA2 for cryptographic hashing (SHA256Cng or SHA256CryptoServiceProvider classes)
Support for the trusted publisher list for managed controls in the browser
Enables a simple elevation model for controls in the browser
New opt-in reflection security model
Apps can reflect on the private members of libraries at the same or lower trust level
Useful for script engine scenarios
NOTES:
http://blogs.msdn.com/shawnfa/
System.Security.crypto
Suite B – Gov. stronger crypto. Modern algorithms. Vista API’s.
ECDSA - Data, code signing.
FIPS – secret key to encrypt data, FIPS – checksum.
Trusted Pub - Object tag in browser for managed control (if .Net is installed), Adding signed control and stick it in trusted list. Built into IE/Orcas. Add cert signed with, to the trusted publisher list on the client. Sign ctrl and deploy cert.
Opt-in: Trusted reflection previously required FT without this. Need to add new opt-in permission to permset. Script engine could be untrusted…
Posted in: .NET Framework| Tags: .NET 3.5 CLR Security Support class crypto list browser suiteASP.NET Dynamic Language Support
Introduction
Dynamic Languages are a class of high-level programming languages that do not rely on static typing. Many decisions that are made at compile time by a statically typed language are instead made at run time by a dynamic language. For example, many dynamic languages use dynamic typing, where an object’s type is determined at run time instead of at compile time. These languages make a trade-off between compile-time type-checking in favor of increased flexibility at run time.There are many good static languages, such as C#, and many good dynamic languages, such as IronPython. The choice of what type of language to use comes down to personal preference and to the nature of the project you’re working on.
Giving ASP.NET users the choice of languages was part of the design since our first version of ASP.NET, and this Dynamic Language Support is just another step in that direction. Unlike other Web platforms that support only one language, the ASP.NET team wants to enable users to choose the language that fits them best.
This Release
This release is compatible with IronPython 2.6 Beta 1. Currently it does not include Language Services Support and project templates. To create a new IronPython ASP.NET WebForms project, simply copy the “examples\hello-webforms” to a new directory and rename the directory to your liking. A redistributed copy of IronPython 2.6 Beta 1 can be found in the “bin” directory; all files except Microsoft.Web.Scripting.dll, the IronPython ASP.NET integration, are from the IronPython 2.6 Beta 1 release.Included in this release are two WebForms examples that are written in IronPython: “hello-webforms” and “album-handler”, which can be found in the “examples” directory. “hello-webforms” is a simple web application that shows PostBack handling, and “album-handler” is a larger web application that creates a photo album from a directory of images and generates thumbnails for them on the fly.
Current Limitations
There are some tooling limitations with this release:- Limited support for designers
- No IntelliSense support.
- No support for ASP.NET MVC. This is planned in the future by extending the IronRuby ASP.NET MVC support: http://github.com/jschementi/ironrubymvc.