locked
HOWTO Have script and codebehind in Global.asax. Or auto launch some compiled C# code. RRS feed

  • Question

  • User-2022643801 posted

    HOWTO Have script and codebehind in Global.asax. Or auto launch some compiled C# code.

     

    Please help,

     

    I want to be able to launch a compiled set of C# code from a DLL in ASP.NET, while still having script in my Global.asax. If it were possible, I'd prefer to use Global.asax for both script and code-behind. Or maybe have the web.config make somethign launch C#/ASP.NET/DLL Code-Behind code whent he application (ASP.NET web site) launches.

     

    If this were possible with Global.asax, then something like below would be an example of what I am looking for:

     

    =============================== Take script from Global.asax ==========================

    <%@ Application Language="C#" %>
    <script runat=server>
    void Application_Start(object sender, EventArgs e)
    {
      Application["This is script setting an app variable..."] = "Something";
    }
    </script>

    ======================================================================================


    ============== Add Code Behind to a compiled DLL fro a Global.asax.cs ================

    <%@ Application Language="C#" CodeBehind="MyGlobal.dll"%>
    <script runat=server>
    </script>
    ======================================================================================


    ========================= Code to the DLL referred above =============================
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    using System.Web.Security;
    using System.Web.SessionState;

    namespace SAIT
    {
      public class Global : System.Web.HttpApplication
      {
        protected void Application_Start(object sender, EventArgs e)
        {
          Application["Set seomthing secret!!!"] = "Secret...";

        }
      }
    }
    ======================================================================================

      If Global.asax is not possible, is there any IIS (Server 2008+) setting that can make IIS launch some code behind code and still let Global.asax/script work?  

     

     

    Friday, March 12, 2010 10:24 AM

Answers

  • User-2022643801 posted

    I figured it out..

     

    In the Global.asax.cs file, declare you Global class as such:

    public class Global : System.Web.HttpApplication
    {
      public Global()
      {
        // Do anything you like....
        // This code get launched first, before anything inside Global.asax...
      }

      // Notice, do not declare any Application_~~~ or Session_~~~ anything, or it will
      // be only used from here...
    }


    Then change the Global.asax as such:

    <%@ Application Language="C#" Inherits="Global" %>
    <script runat=server>
    void Application_Start(object sender, EventArgs e)
    {
      // This script gets called after Global's constructor. Yay!!!
      Application["This is script setting an app variable..."] = "Something";
    }
    </script>

     

     

     

     

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, March 12, 2010 10:56 AM

All replies

  • User-2022643801 posted

    I figured it out..

     

    In the Global.asax.cs file, declare you Global class as such:

    public class Global : System.Web.HttpApplication
    {
      public Global()
      {
        // Do anything you like....
        // This code get launched first, before anything inside Global.asax...
      }

      // Notice, do not declare any Application_~~~ or Session_~~~ anything, or it will
      // be only used from here...
    }


    Then change the Global.asax as such:

    <%@ Application Language="C#" Inherits="Global" %>
    <script runat=server>
    void Application_Start(object sender, EventArgs e)
    {
      // This script gets called after Global's constructor. Yay!!!
      Application["This is script setting an app variable..."] = "Something";
    }
    </script>

     

     

     

     

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, March 12, 2010 10:56 AM
  • User-1237044210 posted

    Hi J-S-B

    I think you should use bootstrapper in Global.asax. Bootstrapper is the class that start when your application run and load all thing that you need. In your case, you will load your .dll file and some thing you want. You can see one axamples at here http://weblogs.asp.net/rashid/archive/2009/02/17/use-bootstrapper-in-your-asp-net-mvc-application-and-reduce-code-smell.aspx

    . So you don't need to coding many LOC in code behind's Global.asax

    Regards

    Tuesday, March 16, 2010 12:38 PM