65.9K
CodeProject is changing. Read more.
Home

ASP.NET with Managed C++

starIconstarIconstarIconstarIconstarIcon

5.00/5 (3 votes)

Aug 20, 2002

CPOL

2 min read

viewsIcon

146307

downloadIcon

1156

Use Managed C++ as your code-behind file for ASP.NET web pages

Sample Image - HelloWorldMC.gif

Introduction

I recently discussed this topic with Chris Maunder on the Message Boards. Chris concluded that ASP.NET does not support Managed C++, well I agree. But it does support MSIL compiled assemblies.

Eventhough the .NET Framework supports many language compilers, ASP.NET has out of the box support for parsing three languages only; C#, VB.NET, JScript. Therefore, we must bypass ASP.NET parser to use a language that ASP.NET does not support. The method we will use to accomplish this is called "code-behind". This method should work for any language that supports a compiler for .NET (e.g. COBOL). I will be demonstrating this technique with the renowned "Hello World" example. This article's only objective is to show you, how to use Managed C++ as code-behind for ASP.NET pages, it doesn't demonstrate any complex C++ coding.

Using Managed C++ as ASP.NET code-behind

First, lets create a simple ASP.NET file called HelloWorldMC.aspx. The most crucial part of this file is Inherits="HelloWorldMC", this will tell ASP.NET to look for an assembly (code-behind) file.

<%@ Page AutoEventWireup="false" Inherits="HelloWorldMC" %>
<html>
    <head>
        <title>HelloWorld MC++</title>
    </head>
    <body>
        <form runat=server ID="Form1">
           <asp:Button id="Button1" runat="server" Text="Click Me Please" 
           OnClick="SayHello" Width="172px"></asp:Button>
        </form>
    </body>
</html>

Next we create the Managed C++ file, as our code-behind file named HelloWorldMC.aspx.cpp, I am following the naming convention of Visual Studio.NET here. It is important to keep the names of your server controls consistent between your .aspx file and your .cpp (code-behind) file. For example, id="Button1" is used in the .aspx file and Button* Button1 is used in the code-benind file, which will bind it to the <asp:Button id="Button1" runat=server></asp:Button> control. Also remember that the function SayHello must be public so the .aspx page can access it.

#using <system.dll>
#using <mscorlib.dll>
#using <system.web.dll>

using namespace System;
using namespace System::Web::UI::WebControls;

public __gc class HelloWorldMC : public System::Web::UI::Page
{
protected:

    Button* Button1;

public:

    void SayHello (Object* sender, EventArgs* e)
    {
        Button1->Text = "Hello MC++ World";	
        return;
    }

};

Compiling the project

Make sure that the HelloWorldMC.aspx and HelloWorldMC.aspx.cpp files are placed in the root of your IIS web server (i.e. C:\Inetpub\wwwroot). Now if the subdirectory bin does not exist in the root, create it (i.e. C:\Inetpub\wwwroot\bin). The following command line syntax will compile and create our code-behind file and place it in the bin. Make sure this is ran from the web server root direcotory.

cl /clr HelloWorldMC.aspx.cpp /link /dll /out:bin\HelloWorldMC.dll

Start up your browser and browse the file, http://localhost/HelloWorldMC.aspx, ASP.NET does not care how the code-behind (assembly) was created, as long as it adheres to the Inherits="HelloWorldMC" in the .aspx file, which tells ASP.NET there is an assembly named HelloWorldMC.dll. Please note that there are advantages to using code-behind techniques in all your ASP.NET programming, to list a couple:

  1. Compilation before execution
  2. Seperation of presentation code from business logic

Obtaining Intellisense for .CPP code-behind file, in VS.NET

My suggestion for obtaining intellisense for your .cpp file is, to add a blank Managed C++ project to a currentley existing web project. I tried creating the .cpp code-behind file first, then adding an existing item to my web project and the .cpp file was added to my web project, but the intellisense would not work properly.