Application_BeginRequest does not seem to run

G

Guest

Hi,

I have set up a new web site application in VS 2005, and have created a
couple of pages which seem to run ok.

I then manually added a file called Global.asax.vb, within a folder that I
also manually created 'App_Code' with the start code as below:

Imports System.Security
Imports System.Security.Principal
Imports System.Web.Security
Imports System.Threading
Imports System.Globalization
Imports System.Configuration

Namespace ASPNET.Test

Public Class [Global]
Inherits System.Web.HttpApplication
Sub Application_BeginRequest(ByVal sender As Object, ByVal e As
EventArgs)

Try

........


However when I try and debug the application the code on the pages seem to
run ok, but the code within my 'Sub Application_BeginRequest' does not seem
to run.

Have I gone about setting up this file in the wrong way, or is there a
setting I need to change to ensure the whole thing runs as an application and
not as individual pages?

Interestingly when I open up VS 2005 before opening an application /
project, there are several recent projects, some of which have a VS Logo to
the left of them, whilst the project in question doesn't it only has a logo
that looks like a folder - Does this mean that it is not configured proerly
as an application, and therefore doesn't even look for a Global.asax file?


Thanks, Mike.
 
S

Steven Cheng[MSFT]

Hello Mike,

Welcome to the MSDN newsgroup.

From your description, I understand you're developing an ASP.NET 2.0/vs
2005 web application and you have manually added a codebehind file for your
application's global.asax component. However, you found the
Application_BeginReqeust handler's code not executed at runtime, correct?

Based on my understanding, as for using codebehind file for global.asax
component in ASP.NET 2.0/vs 2005 application, we need to take care of the
following things:

1. ASP.NET 2.0/VS 2005 by default use inline code model for global.asax,
that means it put the event handlers's code in global.asax file (instead of
a separate code behind). e.g.

===========
<%@ Application Language="C#" %>

<script runat="server">

void Application_Start(object sender, EventArgs e)
{
// Code that runs on application startup
}

........................
</script>
============

Therefore, if you want to add codebehind file for global.asax, you can
manually create a source code file(contains the global class), but you also
need to associate this file with the global.asax file.(see #2)

2. To associate the global.asax file with our codebehind class file, you
can use the @Application directive in global.asax file. e.g.

====in global.asax========
<%@ Application Language="C#" CodeFile="globalcode.cs"
Inherits="globalcode"%>

=====globalcode.cs========
public partial class globalcode : HttpApplication
{
public globalcode()
{

}

void Application_BeginRequest(object sender, EventArgs e)
{

HttpContext.Current.Response.Write("<br/>Application_BeginRequest...........
");
}
=======================

#Note that .net framework 2.0 use partial class to assciate ASP.NET front
page/usercontrol with codebehind, so I define the codebehind class as
partial also(and derived from Httpapplication class).

Also, since I've specified "CodeFile" attribute in global.asax file's "@
Application" directive, we can simply put the codebehind file(globalcode.cs
in this case) in the same folder with global.asax(application root dir).
Thus, at runtime, ASP.NET compiler engine will locate the codebehind
through the directive and attribute setting and compile them together.

As for the problem you encountered, I think it is likely the ASP.NET
runtime doesn't use your global class(for some configuration reason). You
can check the above settings. In addition, we can use the following code
statements in our page's code to detect whethter the ASP.NET application is
using the global.asax (rather than the default HttpAplication class) in our
application:

=============
protected void Page_Load(object sender, EventArgs e)
{
Response.Write("<br/>application class: " +
Context.ApplicationInstance.GetType());
}
===========

If the globa.asax is working, the output should be something like:

=-==============
application class: ASP.global_asax
=============


Hope this helps you some. If there is anything else we can help, please
feel free to let me know.

Regards,

Steven Cheng

Microsoft MSDN Online Support Lead


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

When responding to posts, please "Reply to Group" via your newsreader so
that others may learn and benefit from your issue.

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


This posting is provided "AS IS" with no warranties, and confers no rights.



Get Secure! www.microsoft.com/security
(This posting is provided "AS IS", with no warranties, and confers no
rights.)
 
G

Guest

Steven,

Thank you. You gave the perfect answer, just what I needed!

Not knowing exactly how it worked I just copied the code file and not the
..asax file across from another application.

Thanks for your help. I haven't had much time, but I think it would be
worth my while spending some time going back to learn the basics in .Net!


Cheers, Mike.
 
S

Steven Cheng[MSFT]

Hi Mike,

Thanks for your followup.

I'm glad that those information is of assistance. Also, please always feel
free to post here when there is anything we can help.

Have a good day!

Regards,

Steven Cheng
Microsoft MSDN Online Support Lead


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

When responding to posts, please "Reply to Group" via your newsreader so
that others may learn and benefit from your issue.

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


This posting is provided "AS IS" with no warranties, and confers no rights.



Get Secure! www.microsoft.com/security
(This posting is provided "AS IS", with no warranties, and confers no
rights.)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top