全面介紹開發(fā)ASP.NET
在Visual Studio .NET 2002剛出來時,我就曾經(jīng)聽過同事說過他用C++寫過ASP.NET,不過由于當(dāng)時才剛剛學(xué)C#,還不會C++,所以也沒問他是怎么寫的,一直到最近開始學(xué)C++,發(fā)現(xiàn)在Visual Studio 2005可以用C++/CLI開發(fā)Windows Form,但卻無法開發(fā)ASP.NET,實(shí)在令人遺憾。在網(wǎng)路上也只在Code Project談到在Visual Studio .NET 2002下用Managed C++寫ASP.NET(ASP.NET with Managed C++),但Managed C++和C++/CLI的語法不太一樣,原本的范例無法compile成功,經(jīng)過一段研究之后,終于找到了用C++/CLI撰寫ASP.NET的方式。在這篇文章中,我將一步步的介紹如何用C++/CLI開發(fā)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名稱。也要將<script runat="server"></script>部分刪除。
- <%@ Page AutoEventWireup="true" Inherits="HelloWorld" %>
- <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">- <html xmlns="http://www.w3.org/1999/xhtml">
- <head runat="server">
- <title>Untitled Page</title>
- </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
在左側(cè)選擇CLR,此為.NET platform的Project,右側(cè)選擇CLR Empty Project即可,切記不要選擇Class Library,這樣會多出很多我們不需要的檔案,而且***我們也不會用Visual Studio 2005來compile ,會使用Command Prompt的方式compile。
Step 6:
建立HelloWorld.aspx.cpp。
左側(cè)選擇Code,右側(cè)選擇C++ File(.cpp)
Step 7:
加入C++/CLI程序
C++/CLI對ANSI C++做了些擴(kuò)充,為了和C++內(nèi)建的型別與Class做區(qū)別,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++無法開發(fā)ASP.NET,ISO C++的確不能,但C++/CLI則可以,事實(shí)上,任何.NET下的語言都可以開發(fā)ASP.NET,雖然Visual Studio 2005工具不見的支援,但只要透過一些小技巧,你依然可以用妳喜歡的.NET語言開發(fā)ASP.NET。
(01/27/2007更新)
這篇文章得到很大的回響,大概因?yàn)檫@是世界上***篇使用C++/CLI開發(fā)ASP.NET的討論,不過回到現(xiàn)實(shí),真的該用C++開發(fā)ASP.NET嗎?
老實(shí)說,C++/CLI開發(fā)ASP.NET真的比較麻煩,而且用C++/CLI在執(zhí)行速度也不會比C#快多少,實(shí)務(wù)上還是建議使用C#開發(fā)Web應(yīng)用程式,用C++開發(fā)系統(tǒng)程式(驅(qū)動程式,kernel..),畢竟兩者定位不同。
若你真的還是很堅(jiān)持非用C++不可,以下是我的建議:
采用多層式架構(gòu)
1.Presentation Layer : ASP.NET部分用C#寫,主要是Visual Studio 2005工具支援完整。
2.Business Layer / Data Access Layer:這里可以用C++/CLI寫,這里不牽涉到UI,只牽涉到邏輯和資料,你可以在這里盡情的發(fā)揮C++在OOP,STL,GP上的優(yōu)勢。
3.Database :這里還是要用SQL Server或Oracle。
我這篇文章,主要也是證明C++還是可以寫ASP.NET,但并沒有推薦大家一定要這樣寫,主要還是Visual Studio 2005的支援度不夠,寫起來不方便,若下一個版本Microsoft愿意讓Visual Studio直接支援C++/CLI開發(fā)ASP.NET,若你又是C++忠實(shí)信徒,那就真的推薦用C++開發(fā)ASP.NET了。
另外直得一題的是,C++要開發(fā)ASP.NET,用的是C++/CLI而非ISO C++,雖然C++/CLI包含了所有ISO C++,但還有一些闊充的語法,如pointer變成^, reference變成%...等,當(dāng)然C++/CLI也是相當(dāng)有趣的主題,值得一學(xué)。
【編輯推薦】