Solved

Global.asax and aspx pages in subdirectories

Posted on 2006-07-22
12
1,322 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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Today is the age of broadband.  More and more people are going this route determined to experience the web and it’s multitude of services as quickly and painlessly as possible. Coupled with the move to broadband, people are experiencing the web via …
User art_snob (http://www.experts-exchange.com/M_6114203.html) encountered strange behavior of Android Web browser on his Mobile Web site. It took a while to find the true cause. It happens so, that the Android Web browser (at least up to OS ver. 2.…
This tutorial demonstrates a quick way of adding group price to multiple Magento products.
A company’s greatest vulnerability is their email. CEO fraud, ransomware and spear phishing attacks are the no1 threat to a company’s security. Cybercrime is responsible for the largest loss of money to companies today with losses projected to r…

932 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

12 Experts available now in Live!

Get 1:1 Help Now