Solved

Global.asax and aspx pages in subdirectories

Posted on 2006-07-22
12
1,325 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
Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

 

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
 

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

Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

Question has a verified solution.

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

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…
I recently went through the process of creating a Calendar Control of events with the basis of using a database to keep track of the dates that are selectable, one requirement was to have the selected date pop-up in a simple lightbox.  At first this…
Email security requires an ever evolving service that stays up to date with counter-evolving threats. The Email Laundry perform Research and Development to ensure their email security service evolves faster than cyber criminals. We apply our Threat…
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…

786 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