怎麼在asp.net2.0中使用Global.asax

來源:互聯網
上載者:User


Global.asax in Visual Studio 2005

In Visual Studio 2003, the Global.asax consisted of two files: Global.asax (design-time HTML) and Global.asax.xx (language-specific code-behind). The class was also named Global by default and could be referenced accordingly througout the application. Since Global.asax was an incarnation of HttpApplication at runtime, I added application-level properties and methods to the Global class. The Global.asax in Visual Studio 2005, however, is much different by default. When adding a new 'Global Application Class' to your project, you'll get the Global.asax, but no code-behind. This really isn't a big deal because VS2005 has IntelliSense in the design-time HTML files now, but there are still differences. I want to describe some of those differences here for those that are used to Global.asax circa VS2002/2003.Designer ViewGlobal.asax seems to no longer have a designer view. With this view, you could drag items from Server Explorer, such as Event Log, into your project. I never really used this feature, so that's all I've got to say about that.Importing NamespacesI began coding Global.asax as I had always done in Global.asax.cs (C# is my language of choice), but the using statement for importing namespaces caused a compile error. Instead you have to use the @Import directive: <%@ Import Namespace="System.Workflow.Runtime" %> //這條加進去會出錯,不知道是不是版本不同,可能這篇文章有點老Global Class NameI suppose Global.asax would get compiled into an anonymously named class, but I could not find a class reference that contained the properties/methods I had coded. At this point, I began searching for a better solution to my need for a Global class. My first solution was to create a new class, Global.asax.cs (in the App_Code folder). I then made this file the code-behind for Global.asax with the CodeBehind attribute of the @Application directive: <%@ Application Language="C#" CodeBehind="Global.asax.cs" %> This worked out ok, but something still didn't feel right. The Global class (in Global.asax.cs) inherits System.Object, but the Global.asax still functioned as an instance of HttpApplication because code in Application_Start and Application_End would run accordingly. My second and final attempt normalized the class hierarchy between these two files. In Global.asax.cs, I changed the Global class to inherit HttpApplication. I then replaced the CodeBehind attribute of the @Application directive in Global.asax with the Inherits attribute: <%@ Application Language="C#" Inherits="Global" %>//CodeBehind="Global.asax.cs" 和 Inherits="Global"要放到一起 Now the class hierarchy matched the usage and I had the familiar Global class to program against.

posted @ Monday, November 14, 2005 6:10 AM 

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.