Using ASP.NET Runtime in Desktop Applications

Environment: .NET 1.1


This article explains how to use the ASP.NET runtime in Windows Desktop applications.

This article consists of two parts. The first part explains how to use an ASP.NET script in a Windows Application. The ASP.NET script, when processed, basically generates HTML output, which is normally displayed by a Web browser client such as Internet Explorer or Netscape Navigator.

The second part of the article discusses use of Microsoft WebBrowser control in an application. This article does not cover this topic in very much detail, but provides pointers to relevant information. This part of the article will display the HTML output generated in the first part.

Using ASP.NET Runtime

ASP.NET exposes a well-known interface that can be used to process an ASP.NET Web page request. HttpRuntime, ApplicationHost, and HttpWorkerRequest are important classes involved in this process.

The HttpRuntime is located in the System.Web namespace. It is the entry point of the HTTP pipeline that transforms an aspx request to an HTML page. To transform an aspx page to HTML, its static method ProcessRequest is used. Following is the signature of the ProcessRequest function.

public static void ProcessRequest(
    HttpWorkerRequest wr

Before using HttpRuntime, it should be passed all required information. The HttpWorkerRequest class is used for this purpose. SimpleWorkerRequest is a simple implementation of HttpWorkerRequest that can be used to pass required parameters. The following shows the class' constructor signature.

public SimpleWorkerRequest(
    string page,            // aspx page requested
    string query,           // query string passed to the request
    TextWriter output       // captures HTML output from the
                            // response

The following code snippet can be used to transform an aspx page to an HTML page and send the HTML page to the console. Remember, this code snippet should be run in a new AppDomain created by the client. As explained in the coming sections, the ApplicationHost class can be used for this purpose.

SimpleWorkerRequest = new SimpleWorkerRequest(aspxPage,

It should be noted that the HttpRuntime can be used only in a new AppDomain. The caller of the ASP.NET runtime should create a new AppDomain for the ASP.NET runtime environment, and the ProcessRequest method of the HttpRuntime should be called in the newly created AppDomain. The new AppDomain can be created by using the AppDomain and the Assembly classes in the .NET framework. However, the ApplicationHost class is bundled with the .NET SDK exactly for this task, and simplifies the programming work involved. This article uses the ApplicationHost class. The only method of this class, CreateApplicationHost, is used for this purpose. Its signature is given in the following code snippet.

public static object CreateApplicationHost(
    Type hostType,          // The class instance to be created
                            // in new Appdomain
    string virtualDir,      // Virtual directory of the AppDomain
                            // created
    string physicalDir      // Physical directory of the AppDomain
                            // where the aspx pages are located.

The preceding call loads the assembly containing the class hostType in the new AppDomain created and returns a reference to it. Remember the hostType is created in the newly created AppDomain and only the reference (actually, a proxy) is returned. Its use is shown in the following code snippet.

MyHost host = new ApplicationHost.CreateApplicationHost(

The following section summarizes the steps required to use the ASP.NET runtime in a Windows application.

  1. Create a new AppDomain to call the HttpRuntime.PrecessRequest method on the Web page to be processed. This results in HTML being generated from the ASP.NET Web page. The output is sent to the StreamWriter, passed as a parameter in the HttpRuntime.ProcessRequest function call.
  2. Either process the HTML page directly from the StreamWriter or save the output generated to a file. The save file can be used later. This article saves the HTML output to a file, which is used later.
  3. Use the HTML output generated in your application. This article uses the Microsoft HTML WebBrowser control to display the HTML output.

The following code snippet, from the code included with the article, shows Steps 1 and 2.

host = (MyHost)ApplicationHost.CreateApplicationHost(
host.CreateHtmlPage(webPage, null, m_outFile);

The CreateHtmlPage function of the MyHost class is given below. Note that the HTML output generated will be saved to a file specified by the third parameter. This file will be later displayed by the WebBrowser control.

public void CreateHtmlPage(String webPage,
                           String query,
                           String file)
    StreamWriter stream = new StreamWriter(file);

    SimpleWorkerRequest swr = new SimpleWorkerRequest(webPage,

Using the Microsoft Web Browser Control

Using the Microsoft Web Browser control is explained in the article Using the WebBrowser Control in .NET by Nikhil Dabas. The process is summarized in the following steps.

  1. Import the assemblies from the WebBrowser control, SHDocVw.dll, using the command given below. It will generate two assemblies, AxShDocVw.dll and SHDocVw.dll.
  2. aximp c:\windows\system\shdocvw.dll
  3. Use the imported WebBrowser control in the .NET application. The control to be used is AxSHDocVw.AxWebBrowser.

To use the WebBrowser control in this article, the following code is used.

private AxSHDocVw.AxWebBrowser m_Browser;

// initialize the WebBrowser Control
this.m_Browser = new AxSHDocVw.AxWebBrowser();
this.label = new System.Windows.Forms.Label();
this.txtFile = new System.Windows.Forms.TextBox();
this.cmdOpen = new System.Windows.Forms.Button();
this.m_Browser.Anchor = 
        | System.Windows.Forms.AnchorStyles.Bottom) 
        | System.Windows.Forms.AnchorStyles.Left) 
        | System.Windows.Forms.AnchorStyles.Right)));
this.m_Browser.Enabled = true;
this.m_Browser.Location = new System.Drawing.Point(0, 72);
this.m_Browser.Size = new System.Drawing.Size(360, 232);
this.m_Browser.TabIndex = 0;

The ASP.NET runtime requires the binaries to be located either in GAC or in the bin sub directory of the Web application. The build script included with the code automatically copies the required assemblies in the bin directory. If you get an exception such as "System.IO.FileNotFoundException," check this point.

Running the Application

Use either the command line "UsingAspRuntime.exe AspxFileName" or specify the aspx file in the Filename Text Box and click Open. Two ASP.NET files, test.aspx and test2.aspx, are included with the code.


Download demo project - 50 Kb
Download source - 4 Kb


  • Download Source broken link

    Posted by paxcal on 02/27/2004 08:10am

    Hi, The link to download the source is Pascal

Leave a Comment
  • Your email address will not be published. All fields are required.

Top White Papers and Webcasts

  • Java developers know that testing code changes can be a huge pain, and waiting for an application to redeploy after a code fix can take an eternity. Wouldn't it be great if you could see your code changes immediately, fine-tune, debug, explore and deploy code without waiting for ages? In this white paper, find out how that's possible with a Java plugin that drastically changes the way you develop, test and run Java applications. Discover the advantages of this plugin, and the changes you can expect to see …

  • Between contractors and employees, there is a great deal of risk surrounding the privacy and security of sensitive data in the healthcare industry. When a breach is suspected, the law requires that healthcare organizations quickly launch an investigation to find the source and scope of the issue. These investigations can be quite costly, to say nothing of the penalties for the breach itself and the damage to reputation and brand. This white paper details new affordable software tools that can help healthcare …

Most Popular Programming Stories

More for Developers

Latest Developer Headlines

RSS Feeds