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" %>
<form runat="server" ID="Form1">
<asp:Button id="Button1" runat="server" Text="Click Me Please"
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 namespace System;
using namespace System::Web::UI::WebControls;
public __gc class HelloWorldMC : public System::Web::UI::Page
void SayHello (Object* sender, EventArgs* e)
Button1->Text = "Hello MC++ World";
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:
- Compilation before execution
- 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.