No access to Global.asax methods in ASP.Net 2.0

R

Ronnie

I've created a simple web application using VS2005 Beta 2.

Basically, I've added a Web Form and a Global.asax file.

In my Global.asax, I create a method like:

public static void TestGlobal() {
throw new Exception("Foobar");
}

The problem, however, is that I cannot gain access to this method from the
form class, i.e., this code fails:

protected void Page_Load(object sender, EventArgs e) {
Global.TestGlobal(); // Intellisense claims no class Global exist.
}

You would write like this in ASP.Net 1.1, but how would you access methods,
enums, etc in Global.asax in ASP.Net 2.0?

Ronnie
 
B

Brock Allen

If the code's all in the ASAX (instead of a ASAX codebehind), try:

Global_asax.TestGlobal();
 
B

Brock Allen

It's not a namespace, it's the name of the class generated from the global.asax.
I just tested it locally and it was functional. I'm wondering what might
be different....
 
B

Brock Allen

Ah, I bet you're trying it from codebehind? My tests were all from codeinside
in my ASPX.

So, you can build a codebehind for your global.asax like the "olden days"
:). Make a class in the App_Code folder called "Global" that inherits HttpApplication.
In your global.asax add an Inherits="Global" directove. Then your codebehind
classes can see/use the Global class in App_Code.

Now having done all of that, I'm not sure why putting code there is any different
or any more useful than simply putting that code into its own class (not
global.asax related) in App_Code. What's your motivation for making that
code in global.asax (I've lost the original post)?
 
J

Juan T. Llibre

Why would you want to access methods which
*already* are run automatically in global.asax ?

If you want to throw an exception in a page, use :

<script runat="server">
void Error_Click(Object sender, EventArgs e) {
throw new Exception();
}
</script>

And, in the body, include :

<input type="submit" OnServerClick="Error_Click" Value="Generate An Error"
runat="server"/>
 
R

Ronnie

You're right. If you create a code behind Global.asax.cs and put it under
App_Code, Intellisense picks it up, and you now have access to its members.

It appears you cannot add any of your own methods (or enums, etc) to the
inline version created through the VS 2005 add file wizard. Probably
because the inline version doesn't define the Global class.

Ronnie
 
B

Brock Allen

It appears you cannot add any of your own methods (or enums, etc) to
the inline version created through the VS 2005 add file wizard.
Probably because the inline version doesn't define the Global class.

You can and the inline does define the class, it's just that the codebehind
classes don't reference that generated assembly. That's why when I was working
with the all codeinside model it was working for me as they do reference
the generated assembly.
 
R

Ronnie

It's just, moving the code behind Global.asax.cs file to App_Code isn't
really a good choice for me at the moment (although I don't seem to have
much of a choice).

All my troubles began when I started migrating a medium sized web
application from .Net 1.1 to 2.0.

Microsoft's idea of the App_Code directory storing the model, as I
understand it, isn't that great for existing applications. If parts of the
model references parts of the GUI, you're in for trouble, because App_Code
contained classes are not allowed to access outside classes.

This will break a lot of application, and require a lot of work to migrate
properly.

My initial solution, after running the migration wizard, was to move all
files out of the App_Code directory back into the parent directory
(retaining the directory structure). This way, the directory structure now
resembles that of the .Net 1.1 application, and I'm not using the App_Code
directory.

So, moving Global.asax.cs to App_Code just gives me more trouble as
Global.asax.cs references classes that are not in App_Code. Of course, you
can move these classes to App_Code, but they reference other classes, that
reference other classes, and so on.

The optimal solution would be to have ones application running under .Net
2.0 and then optionally move code to App_Code as you get your code
refactored.

Ronnie
 
B

Brock Allen

So, moving Global.asax.cs to App_Code just gives me more trouble as
Global.asax.cs references classes that are not in App_Code. Of course,
you can move these classes to App_Code, but they reference other
classes, that reference other classes, and so on.

We still haven't established why the code is in global.asax in the first
place if it's simply meant to be helper code accessible to the entire application.
I probabaly wouldn't have put it there using the v1.1 model, so maybe this
is an edge case that wasn't considered when they were architecting v2.0.
I understand this is existing code, so perhaps you're not in a position to
rearchitect.
 

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

Members online

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top