Global.asax ohne Code-Behind

by Sven 25. July 2006 08:34
Zur Realisierung von ernsthaften Webanwendungen sollte man eigentlich immer das Code-Behind Modell vorziehen, anstatt den Code direkt in eine aspx Seite zu kodieren. Allerdings habe ich nicht schlecht gestaunt, als ich für eine neue Anwendung in Visual Studio eine Global.asax anlegen wollte: Es wird heifür immer Inline-Code verwendet. Auch gibt es keine Option beim Anlegen der Global.asax, um eine Code-Behind zu erzeugen. Abhilfe: manuelles Hinzufügen der entsprechenden Code-Behind Datei:

1.
<%@ Application Language="C#" Inherits="Global" %>
zur Global.asax hinzufügen. Alles andere rauskopieren.

2. Im App_Code Ordner eine Klasse Global.cs anlegen

3. In Global.cs die Klassendefinition ändern:
public partial class Global : System.Web.HttpApplication

4. Die folgenden Methoden zur Global.cs hinzufügen:
    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.
    }

Siehe auch Ross W Nelson's Blog: Fixing the Global.asax in ASP.NET 2.0

Tags:

ASP.NET

Add comment

  Country flag

biuquote
  • Comment
  • Preview
Loading

Page List

My latest Flickr Photos

www.flickr.com

Shared items - Google Reader

Instagram Pictures