Click here to Skip to main content
Click here to Skip to main content

ASP.NET with Managed C++

By , 19 Aug 2002
Rate this:
Please Sign up or sign in to vote.

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.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here

About the Author

Soliant
Software Developer (Senior)
United States United States
No Biography provided

Comments and Discussions

 
QuestionCould not load file or assembly 'TradeQueueApi' or one of its dependencies. An attempt was made to load a program with an incorrect format. Pinmemberardyllesdavid23-Aug-07 21:35 
QuestionWhy not just refrence a managed dll? Pinmemberrj4518-May-07 7:55 
Questionproblems with classes PinmemberSaturday3AM6-Mar-06 23:17 
Generalcannnot compile the cpp Pinmemberchenen16-May-04 17:47 
GeneralRe: cannnot compile the cpp PinmemberSoliant16-May-04 18:05 
GeneralRe: cannnot compile the cpp Pinmemberchenen17-May-04 4:02 
GeneralLooking for more on this topic PinmemberMike Seck29-Nov-03 9:06 
GeneralRe: Looking for more on this topic PinsussAnonymous30-Nov-03 16:01 
GeneralKnown problems with this scenario PinsitebuilderNick Hodapp (MSFT)26-Aug-02 7:10 
GeneralRe: Known problems with this scenario PinmemberSoliant26-Aug-02 22:32 
GeneralCool PineditorJames T. Johnson20-Aug-02 8:58 
GeneralRe: Cool PinmemberSoliant20-Aug-02 9:47 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Mobile
Web03 | 2.8.140415.2 | Last Updated 20 Aug 2002
Article Copyright 2002 by Soliant
Everything else Copyright © CodeProject, 1999-2014
Terms of Use
Layout: fixed | fluid