本文标签:开发ASP.NET
在Visual Studio .NET 2002刚出来时,我就曾经听过同事说过他用C++写过ASP.NET,不过由于当时才刚刚学C#,还不会C++,所以也没问他是怎么写的,一直到最近开始学C++,发现在Visual Studio 2005可以用C++/CLI开发Windows Form,但却无法开发ASP.NET,实在令人遗憾 。在网路上也只在Code Project谈到在Visual Studio .NET 2002下用Managed C++写ASP.NET(ASP.NET with Managed C++),但Managed C++和C++/CLI的语法不太一样,原本的范例无法compile成功,经过一段研究之后,终于找到了用C++/CLI撰写ASP.NET的方式 。在这篇文章中,我将一步步的介绍如何用C++/CLI开发ASP.NET程式 。
Step 1: 建立Web Site 首先,建立一个新的Web Site,由于Visual Studio 2005在ASP.NET没支持C++,所以建立Web Site时,先随便选一个语言建立 。
Step 2: 建立Web Form 建立一个Web Form名为HelloWorld.aspx,请不要选择Place code in separate file,这样Visual Studio 2005会将Event Handler放在aspx档中,可以让aspx.cpp省掉event宣告的程式 。
Step 3: 加入GUI 使用Web Form Designer做出以下的介面 。
Step 4: 修改HelloWorld.aspx 在Page Directive部分,将Language=”C#”删除,加上AutoEventWireup="true" Inherits="HelloWorld",HelloWord为C++的Class名称 。也要将部分删除 。 - <%@ Page AutoEventWireup="true" Inherits="HelloWorld" %>
-
- "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"> <head runat="server"> <title>Untitled Pagetitle> head> <body> <form id="form1" runat="server"> <div> Using C++/CLI in ASP.NET<br /> <br /> <asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Text="Button" /> <asp:Label ID="Label1" runat="server" Text="Label">asp:Label>div> form> body> html>
Step 5: 建立C++ project 在左侧选择CLR,此为.NET platform的Project,右侧选择CLR Empty Project即可,切记不要选择Class Library,这样会多出很多我们不需要的档案,而且最后我们也不会用Visual Studio 2005来compile ,会使用Command Prompt的方式compile 。

Step 6: 建立HelloWorld.aspx.cpp 。 左侧选择Code,右侧选择C++ File(.cpp)
Step 7: 加入C++/CLI程序 C++/CLI对ANSI C++做了些扩充,为了和C++内建的型别与Class做区别,Managed的Class需加上ref modifier,而Managed的Object要加上^ 。最重要的,IDE支援Intellisense方式写ASP.NET 。
C++/CLI / HelloWorld.aspx.cpp - #using <system.dll>
- #using <mscorlib.dll>
- #using <system.web.dll>
-
- using namespace System;
- using namespace System::Web::UI::WebControls;
-
- public ref class HelloWorld : public System::Web::UI::Page {
- protected:
- Button ^Button1;
- Label ^Label1;
-
- public:
- void Button1_Click(Object ^sender, EventArgs ^e) {
- this->Label1->Text = "Hello World";
- return;
- }
- };
Step 8: 编译程式 使用Visual Studio 2005 Command Prompt编译C++/CLI 。

Step 9: Deploymemt 最后只要将HelloWorld.aspx放到c:\Inetpub\wwwroot\下,HelloWorld.dll放到c:\Inetpub\wwwroot\bin\下,就完成deployment 。
Conclusion 很多人说C++无法开发ASP.NET,ISO C++的确不能,但C++/CLI则可以,事实上,任何.NET下的语言都可以开发ASP.NET,虽然Visual Studio 2005工具不见的支援,但只要透过一些小技巧,你依然可以用妳喜欢的.NET语言开发ASP.NET 。
(01/27/2007更新) 这篇文章得到很大的回响,大概因为这是世界上第一篇使用C++/CLI开发ASP.NET的讨论,不过回到现实,真的该用C++开发ASP.NET吗?
老实说,C++/CLI开发ASP.NET真的比较麻烦,而且用C++/CLI在执行速度也不会比C#快多少,实务上还是建议使用C#开发Web应用程式,用C++开发系统程式(驱动程式,kernel..),毕竟两者定位不同 。
若你真的还是很坚持非用C++不可,以下是我的建议: 采用多层式架构 1.Presentation Layer : ASP.NET部分用C#写,主要是Visual Studio 2005工具支援完整 。 2.Business Layer / Data Access Layer:这里可以用C++/CLI写,这里不牵涉到UI,只牵涉到逻辑和资料,你可以在这里尽情的发挥C++在OOP,STL,GP上的优势 。 3.Database :这里还是要用SQL Server或Oracle 。
我这篇文章,主要也是证明C++还是可以写ASP.NET,但并没有推荐大家一定要这样写,主要还是Visual Studio 2005的支援度不够,写起来不方便,若下一个版本Microsoft愿意让Visual Studio直接支援C++/CLI开发ASP.NET,若你又是C++忠实信徒,那就真的推荐用C++开发ASP.NET了 。
另外直得一题的是,C++要开发ASP.NET,用的是C++/CLI而非ISO C++,虽然C++/CLI包含了所有ISO C++,但还有一些阔充的语法,如pointer变成^, reference变成%...等,当然C++/CLI也是相当有趣的主题,值得一学 。
|