Using the ASP.NET Runtime to Extend Desktop Applications with HTML Scripts

he ASP.NET runtime in .NET provides the ability to create rich HTML content for your applications dynamically in your desktop applications. It also provides a powerful mechanism for extending the functionality of applications through script code.

A few issues back (CoDe Magazine, Nov/Dec 2002) I introduced the topic of dynamic code execution, which is not trivial in .NET. My article generated questions from CoDe readers about how to use this technology in more sophisticated applications. Most of the questions centered around the apparently intriguing topic of ‘executing’ script pages that use ASP-style syntax. My “Dynamically Executing Code in .NET” article was so long that I didn’t have enough room to add an extensive example of how to apply this technology. I will do so this month by rehashing this subject as I show you another more powerful mechanism that’s built into the .NET Framework to provide an ASP.NET-style scripting host for client applications.

Hosting the ASP.NET Runtime
Microsoft made the .NET Framework very flexible, especially in terms of the various sub-systems that make up the core system services. Did you know that you can host the ASP.NET scripting runtime in your own applications? This has several benefits over the ASP-style parsing approach I featured in my last article.

Microsoft ships the ASP.NET runtime in the .NET Framework and has made the ASP.NET runtime a system component so you don’t have to install anything separately. The runtime is much more powerful than the simple script parser I previously introduced because the runtime supports just about everything that ASP.NET supports for Web pages including all installed and registered languages and ASP.NET-style Web Forms syntax. You can use the runtime to determine if you’ve previously compiled a page so you don’t have to recompile it each time. The ASP.NET runtime handles updates to pages automatically, and as an especially nice bonus you can debug your script pages using the Visual Studio .NET debugger.

As always with .NET internals, though, this power comes with a price?overhead and complexity. Visual Studio .NET offers a number of non-obvious ways to accomplish seemingly simple tasks, including passing parameters or leaving the runtime idle for a while. I’ll introduce a set of classes that simplify this process down to a few lines of code. I’ll also show you the key things that you need to know and implement.

You’ll find that you can call the ASP.NET runtime from any .NET applications. Follow these three major steps:

1. Set up the runtime environment.

You’ll tell the runtime which directory to use as its base directory for a Web application (like a virtual directory on a Web Server except here it will be all local files) and you’ll set up a new AppDomain that the runtime can execute in.The ASP.NET runtime executes in another AppDomain and all information transmitted between your app and it run over the remoting features of .NET.

2. Create the script page.

You will create a single page that contains ASP.NET code. This means you can create pages that contain <% %>, <%= %>, and

Overview

Recent Articles: