?
Solved

Global.asax and aspx pages in subdirectories

Posted on 2006-07-22
12
Medium Priority
?
1,353 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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

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 250 total points
ID: 17171371
Yeah, you've got a lot of complex things going on there.

Bob
0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

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

A quick way to get a menu to work on our website, is using the Menu control and assign it to a web.sitemap using SiteMapDataSource. Example of web.sitemap file: (CODE) Sample code to add to the page menu: (CODE) Running the application, we wi…
Problem Hi all,    While many today have fast Internet connection, there are many still who do not, or are connecting through devices with a slower connect, so light web pages and fast load times are still popular.    If your ASP.NET page …
Are you ready to place your question in front of subject-matter experts for more timely responses? With the release of Priority Question, Premium Members, Team Accounts and Qualified Experts can now identify the emergent level of their issue, signal…
Is your data getting by on basic protection measures? In today’s climate of debilitating malware and ransomware—like WannaCry—that may not be enough. You need to establish more than basics, like a recovery plan that protects both data and endpoints.…
Suggested Courses

850 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