2011-05-19 36 views

回答

31

這是因爲您創建了網站而不是Web應用程序。我建議你使用precomipled Web應用程序模型,但如果你需要使用一個網站,你可以做到以下幾點:

~/Global.asax

<%@ Application CodeFile="Global.asax.cs" Inherits="AppName.MyApplication" Language="C#" %> 

~/Global.asax.cs

namespace AppName 
{ 
    public partial class MyApplication : System.Web.HttpApplication 
    { 
     protected void Application_Start() 
     { 
     } 
    } 
} 

現在重新打開網站在VS.

+1

+1有趣。爲什麼這種行爲是這樣設定的? – naveen 2011-05-19 08:46:52

+2

@naveen,這是WebSite與Web應用程序模型之間的差異之一。 – 2011-05-19 08:49:14

+0

不需要。謝謝。當asp.net相信代碼隱藏模型時,我很好奇爲什麼它沒有按設計出現。加上我發現導入命名空間的快捷方式也不會在那裏工作。這就是爲什麼我問這個問題。 – naveen 2011-05-19 08:57:34

5

是@ Darin's答案是對的,可以在Web應用程序中看到cs/vb文件,但在網站中不能有單獨的cs/vb文件。

Global.asax中沒有一個CS文件,但您可以編寫代碼....

<script runat="server"> 

void Application_Start(object sender, EventArgs e) 
{ 
    // Code that runs on application startup 
} 

</script> 
+0

+ 1耶。我知道。我想知道爲什麼沒有CS時CNTRL +。無法導入命名空間。 :) – naveen 2011-05-19 08:48:21

-1

他們這兩個文件是相同的「Global.asax的」涉及到網站和「Global.asax中。 cs「涉及web應用程序。編碼標準在它們兩者中都是相同的

3

實際上在VS 2010標準網站中,Global.asax的設計文件後面沒有代碼。那麼該怎麼辦?你必須像這樣使用內聯代碼模型。

<%@ Application Language="C#" %> 

<%@ Import Namespace="System.Web.Routing" %> 

<script Language="C#" RunAt="server"> 

    void Application_Start(object sender, EventArgs e) 
    { 
    } 

    void Application_End(object sender, EventArgs e) 
    { 
    } 

    void Application_Error(object sender, EventArgs e) 
    { 
    } 

    void Session_Start(object sender, EventArgs e) 
    { 
    } 

    void Session_End(object sender, EventArgs e) 
    { 
    } 
</script> 
+0

這是不正確的。您只需將'Inherits =「Global」'和'CodeBehind =「path/to/Global.cs」''屬性添加到Global.asax文件。 – 2011-12-20 23:52:58

+0

你有沒有嘗試過,如果不是,那麼只是創建一個標準的網站項目,但不是網絡應用程序... – 2011-12-23 01:29:08

+0

是的,我已經嘗試過了。您可以在Web站點和Web應用程序項目中爲Global.asax設置代碼隱藏文件。 – 2011-12-23 13:08:55

-1

你並不需要創建在創建新的Web應用程序或網站,因爲一個新的glabal庫類,應用程序本身已經有一個項目在解決方案資源管理器命名的global.asax.cs。

2

按下Ctrl + Shift + A,然後從列表

+0

這是global.asax,而不是global.asax.cs。 – 2014-03-27 14:12:39

0

添加全局應用程序類序言

我不大明白Darin的答案,因爲他沒有保持標準與全球的命名上課,儘可能尊重。我需要一個針對VS2005的解決方案,而不是2010年,但我相信這個解決方案永遠不會少。

在asp.net 2.0

刪除任何當前的Global.asax和Global.cs文件或試圖解決這一問題添加代碼隱藏的Global.asax文件。好的,一旦離開,進入項目的根文件夾,右鍵單擊並選擇添加新項目...

選擇Global Class並單擊OK

現在回到根文件夾再次,右鍵單擊並選擇一個新的類

名稱該類 - Global.cs

是的 - 允許將其保存在app_code文件夾內。不要說NO,並允許它放在根文件夾中。它必須在的app_code文件夾中。

編輯Global.asax文件並剪切(剪切/粘貼)<script>標記中的代碼。該文件應該是這樣的,並添加繼承標籤..

<%@ Application Language="C#" Inherits="Global" %> 

<script runat="server"> 
    //do not put any code here 
</script> 

轉到的app_code的global.cs文件並粘貼您的Global.asax文件中切出的代碼。

中的app_code的global.cs文件現在應該是這樣的......

/// <summary> 
/// Summary description for Global 
/// </summary> 

public class Global : System.Web.HttpApplication 

{ 

     public Global() 

     { 

       // 

       // TODO: Add constructor logic here 

       // 

     } 



    void Application_Start(object sender, EventArgs e) 

    { 

     // Code that runs on application startup 



    } 



    void Application_End(object sender, EventArgs e) 

    { 

     // Code that runs on application shutdown 



    } 



    void Application_Error(object sender, EventArgs e) 

    { 

     // Code that runs when an unhandled error occurs 



    } 



    void Session_Start(object sender, EventArgs e) 

    { 

     // Code that runs when a new session is started 



    } 



    void Session_End(object sender, EventArgs e) 

    { 

     // Code that runs when a session ends. 

     // Note: The Session_End event is raised only when the sessionstate mode 

     // is set to InProc in the Web.config file. If session mode is set to StateServer 

     // or SQLServer, the event is not raised. 



    } 

} 

現在我們有Global.asax文件和代碼與類名的文件的Global.asax.cs後面。通過這樣做,我們可以繼續訪問網站中任何地方的靜態變量。