Solved

Global.asax and aspx pages in subdirectories

Posted on 2006-07-22
12
1,319 Views
Last Modified: 2012-06-21
Hello. I have this Global.asax file. It's placed on the root of my webpage. I have a problem with getting  protected void Application_BeginRequest(object sender, EventArgs e) to fire.

Aspx pages placed in the root folder, where the Global.asax file is does fire the Application_BeginRequest, but aspx pages that is in subdirectories does not fire it. Why and how to fire with these?


<%@ Application Language="C#" %>
<%@ Import Namespace="util" %>

<script runat="server">
   
    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)
    {
    }

    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.

    }

    protected void Application_BeginRequest(object sender, EventArgs e)
    {
        // Code that runs when a new session is started
        if (Application["LastScheduledRun"] == null)
        {
            Application["LastScheduledRun"] = DateTime.Now;
            //Label1.Text = "No send. First time." + Application["LastScheduledRun"];
            Email mail = new Email();
            mail.SendMail();
        }
        else
        {
            //Label1.Text += "Will run at first hit after 00:00 O'clock." + (((TimeSpan)(DateTime.Now - Convert.ToDateTime(Application["LastScheduledRun"]))).Minutes);
        }

        if ((((TimeSpan)(DateTime.Now - Convert.ToDateTime(Application["LastScheduledRun"]))).Minutes) > 0)
        {
            Email mail = new Email();
            mail.SendMail();

            Application.Lock();
            Application["LastScheduledRun"] = DateTime.Now;
            Application.UnLock();
            //Label1.Text = "Mail is sent at:" + Application["LastScheduledRun"];
        }        
    }
</script>
0
Comment
Question by:m-jansen
  • 7
  • 5
12 Comments
 
LVL 96

Expert Comment

by:Bob Learned
ID: 17160854
Is this 2.0 with the Web Site model or the Web Application model?

Bob
0
 

Author Comment

by:m-jansen
ID: 17161295
2.x with the Web Site model
0
 
LVL 96

Expert Comment

by:Bob Learned
ID: 17161761
If you are using Server.Transfer to redirect to the page, it doesn't fire, but for Response.Redirect is does.

Bob
0
 

Author Comment

by:m-jansen
ID: 17167983
I thought when all kinds of all aspx pages in the project loaded/requested that Application_BeginRequestwould fire.
0
 

Author Comment

by:m-jansen
ID: 17167990
.....in the project was loaded/requested ......
0
 
LVL 96

Expert Comment

by:Bob Learned
ID: 17168611
Here is a possible explanation:

Using server.Transfer() from Application_BeginRequest harm session
http://weblogs.asp.net/ngur/archive/2004/06/24/164771.aspx

Well I know it little bit twisted but If it happened to one of us, probably it could be happened to others. If for some reason you need to catch request in pipeline before page is called and you choose to use Application BeginRequest event. Using server transfer to transfer request to other page from that event will end up without session object and session cookies. Doing so you bypass regular request pipeline and simply don’t call session module who responsible for sessions handling.

You can overcome this behavior by catching application PreRequestHandlerExecute which occurred before calling handlers such as page or web service. At this pipeline step session has been initializing and you won't get session buggy behavior.

Bob
0
Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 

Author Comment

by:m-jansen
ID: 17169316
I don't need to redirect to another page in global.asax.... if it's that we are talking about?
0
 
LVL 96

Expert Comment

by:Bob Learned
ID: 17170284
No, I am talking about how you get your pages in sub-folders to show, since they are the startup page.  How are you changing to those pages?  Server.Transfer?  Response.Redirect?

Bob
0
 

Author Comment

by:m-jansen
ID: 17170369
I enter the link directly in the address field in the web browser or click on a link on a web page. (also html pages)
0
 

Author Comment

by:m-jansen
ID: 17171212
hmm, looks like all my aspx pages fire Application_BeginRequest now....
0
 

Author Comment

by:m-jansen
ID: 17171217
Think I was messing with something. Have not figured out yet what it was
0
 
LVL 96

Accepted Solution

by:
Bob Learned earned 125 total points
ID: 17171371
Yeah, you've got a lot of complex things going on there.

Bob
0

Featured Post

Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

Join & Write a Comment

Sometimes in DotNetNuke module development you want to swap controls within the same module definition.  In doing this DNN (somewhat annoyingly) swaps the Skin and Container definitions to the default admin selections.  To get around this you need t…
In .NET 2.0, Microsoft introduced the Web Site.  This was the default way to create a web Project in Visual Studio 2005.  In Visual Studio 2008, the Web Application has been restored as the default web Project in Visual Studio/.NET 3.x The Web Si…
This video gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're looking for how to monitor bandwidth using netflow or packet s…
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.

743 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now