By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,838 Members | 1,675 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,838 IT Pros & Developers. It's quick & easy.

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

P: n/a
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
Nov 19 '05 #1
Share this Question
Share on Google+
10 Replies


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

Global_asax.TestGlobal();

-Brock
DevelopMentor
http://staff.develop.com/ballen
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


Nov 19 '05 #2

P: n/a
Global_asax isn't a valid namespace either, so that doesn't work.

Ronnie

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

Global_asax.TestGlobal();

-Brock
DevelopMentor
http://staff.develop.com/ballen


Nov 19 '05 #3

P: n/a
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....

-Brock
DevelopMentor
http://staff.develop.com/ballen
Global_asax isn't a valid namespace either, so that doesn't work.

Ronnie

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

Global_asax.TestGlobal();

-Brock
DevelopMentor
http://staff.develop.com/ballen


Nov 19 '05 #4

P: n/a
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)?

-Brock
DevelopMentor
http://staff.develop.com/ballen
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....

-Brock
DevelopMentor
http://staff.develop.com/ballen
Global_asax isn't a valid namespace either, so that doesn't work.

Ronnie

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

Global_asax.TestGlobal();

-Brock
DevelopMentor
http://staff.develop.com/ballen


Nov 19 '05 #5

P: n/a
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"/>


Juan T. Llibre
ASP.NET MVP
http://asp.net.do/foros/
Foros de ASP.NET en Español
Ven, y hablemos de ASP.NET...
======================

"Ronnie" <r_******@yahoo.com> wrote in message
news:eB**************@TK2MSFTNGP09.phx.gbl...
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

Nov 19 '05 #6

P: n/a
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

Brock Allen wrote:
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)?

-Brock
DevelopMentor
http://staff.develop.com/ballen
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....

-Brock
DevelopMentor
http://staff.develop.com/ballen
Global_asax isn't a valid namespace either, so that doesn't work.

Ronnie

Nov 19 '05 #7

P: n/a
> 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.

-Brock
DevelopMentor
http://staff.develop.com/ballen


Nov 19 '05 #8

P: n/a
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

Brock Allen wrote:
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.

-Brock
DevelopMentor
http://staff.develop.com/ballen


Nov 19 '05 #9

P: n/a
re:
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.
That makes sense.

Have you suggested that, or filed a bug report,
at the MSDN Product Feedback Center ?

http://lab.msdn.microsoft.com/productfeedback/

You should.

Juan T. Llibre
ASP.NET MVP
http://asp.net.do/foros/
Foros de ASP.NET en Español
Ven, y hablemos de ASP.NET...
======================

"Ronnie" <r_******@yahoo.com> wrote in message
news:%2****************@TK2MSFTNGP10.phx.gbl... 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

Brock Allen wrote:
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.

-Brock
DevelopMentor
http://staff.develop.com/ballen

Nov 19 '05 #10

P: n/a
> 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.

-Brock
DevelopMentor
http://staff.develop.com/ballen

Nov 19 '05 #11

This discussion thread is closed

Replies have been disabled for this discussion.