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

What happened to the Global class code behing in 2.0?

P: n/a
Just starting to move to ASP.NET 2.0 and having trouble with the Global.asax
code file.

In 1.1 I could have a code behind file for the global.asax file. This allow
for shared variables of the Global class. Note: I use these shared variables
for read only values that are set at application start.

It would appear the 2.0 doesn't like you to use shared variables in the
global class. How do I convert my 1.1 applications to 2.0 without the
ability to have these shared variables?

Why would Microsoft get rid of the global.asax code behind? There must a
reason and a good conversion method (I hope).
--
Pat B
BCC Software, Inc
A BÖWE BELL + HOWELL COMPANY
Jul 16 '07 #1
Share this Question
Share on Google+
15 Replies


P: n/a
It's not the default any more, but you can still use it.

Read this article :
http://www.xerratus.com/2006/10/20/N...arateFile.aspx

You need to place all your global.asax code in global.asax.cs, in the App_Code directory

You must include this at the top of global.asax.cs :

public class Global : System.Web.HttpApplication

....and include this directive in global.asax :
<%@ Application Language="C#" CodeBehind="Global.asax.cs" Inherits="Global" %>

the same for vb :
<%@ Application Language="VB" CodeBehind="Global.asax.vb" Inherits="Global" %>

Download a complete example from :

http://www.xerratus.com/ct.ashx?id=c...iteExample.zip

Juan T. Llibre, asp.net MVP
asp.net faq : http://asp.net.do/faq/
foros de asp.net, en espaol : http://asp.net.do/foros/
======================================

"PatB" <Pa**@community.nospamwrote in message news:45**********************************@microsof t.com...
Just starting to move to ASP.NET 2.0 and having trouble with the Global.asax
code file.

In 1.1 I could have a code behind file for the global.asax file. This allow
for shared variables of the Global class. Note: I use these shared variables
for read only values that are set at application start.

It would appear the 2.0 doesn't like you to use shared variables in the
global class. How do I convert my 1.1 applications to 2.0 without the
ability to have these shared variables?

Why would Microsoft get rid of the global.asax code behind? There must a
reason and a good conversion method (I hope).
--
Pat B
BCC Software, Inc
A BWE BELL + HOWELL COMPANY

Jul 16 '07 #2

P: n/a
Please never assume anything and ALWAYS post the exact behavior you see
(runtime error, compile time error, exact error message).
Of course if we try, it will work as we are not in the same condition (my
guess would be a namespace change ?)

My personal preference would be to use a specific class (also the
configuration class provides an API that will expose values stored in your
web application config class, see WebConfigurationManager).

--
Patrice

"PatB" <Pa**@community.nospama crit dans le message de news:
45**********************************@microsoft.com...
Just starting to move to ASP.NET 2.0 and having trouble with the
Global.asax
code file.

In 1.1 I could have a code behind file for the global.asax file. This
allow
for shared variables of the Global class. Note: I use these shared
variables
for read only values that are set at application start.

It would appear the 2.0 doesn't like you to use shared variables in the
global class. How do I convert my 1.1 applications to 2.0 without the
ability to have these shared variables?

Why would Microsoft get rid of the global.asax code behind? There must a
reason and a good conversion method (I hope).
--
Pat B
BCC Software, Inc
A BWE BELL + HOWELL COMPANY

Jul 16 '07 #3

P: n/a
Juan,

I implemented the work-around and it appears to be working.

In my 1.1 code I can reference these shared variables by
Global.VariableName. In the 2.0 code it appears that I need to reference
then with two global references like this: Global.Global.VariableName.

Should it be like this? Is something wrong? This will mean when converting
my 1.1. apps I'll have to make a lot of changes to change the Global.
reference to Global.Global.

Does anyone know the reasoning why Microsoft removed the code behind model
for the global.asax?
--
Pat B
BCC Software, Inc
A BÖWE BELL + HOWELL COMPANY
"Juan T. Llibre" wrote:
It's not the default any more, but you can still use it.

Read this article :
http://www.xerratus.com/2006/10/20/N...arateFile.aspx

You need to place all your global.asax code in global.asax.cs, in the App_Code directory

You must include this at the top of global.asax.cs :

public class Global : System.Web.HttpApplication

....and include this directive in global.asax :
<%@ Application Language="C#" CodeBehind="Global.asax.cs" Inherits="Global" %>

the same for vb :
<%@ Application Language="VB" CodeBehind="Global.asax.vb" Inherits="Global" %>

Download a complete example from :

http://www.xerratus.com/ct.ashx?id=c...iteExample.zip

Juan T. Llibre, asp.net MVP
asp.net faq : http://asp.net.do/faq/
foros de asp.net, en español : http://asp.net.do/foros/
======================================

"PatB" <Pa**@community.nospamwrote in message news:45**********************************@microsof t.com...
Just starting to move to ASP.NET 2.0 and having trouble with the Global.asax
code file.

In 1.1 I could have a code behind file for the global.asax file. This allow
for shared variables of the Global class. Note: I use these shared variables
for read only values that are set at application start.

It would appear the 2.0 doesn't like you to use shared variables in the
global class. How do I convert my 1.1 applications to 2.0 without the
ability to have these shared variables?

Why would Microsoft get rid of the global.asax code behind? There must a
reason and a good conversion method (I hope).
--
Pat B
BCC Software, Inc
A BÖWE BELL + HOWELL COMPANY


Jul 16 '07 #4

P: n/a
there is no need for a codebehind file. you just put the code in
global.asax file. as there is nothing but the code in the global.asax
file, 2 files don't make much sense.

-- bruce (sqlwork.com)


PatB wrote:
Just starting to move to ASP.NET 2.0 and having trouble with the Global.asax
code file.

In 1.1 I could have a code behind file for the global.asax file. This allow
for shared variables of the Global class. Note: I use these shared variables
for read only values that are set at application start.

It would appear the 2.0 doesn't like you to use shared variables in the
global class. How do I convert my 1.1 applications to 2.0 without the
ability to have these shared variables?

Why would Microsoft get rid of the global.asax code behind? There must a
reason and a good conversion method (I hope).

Jul 16 '07 #5

P: n/a
If you're "just starting" to move into ASP.NET 2.0, then it's not too late to
start developing good habits. One that I've gotten is NOT to use the Web Site
Project model, but the Web Application Project model for building my "stuff".
The original global.asax codebehind arrangement is preserved. My humble
opinion, of course.
-- Peter
Site: http://www.eggheadcafe.com
UnBlog: http://petesbloggerama.blogspot.com
BlogMetaFinder(BETA): http://www.blogmetafinder.com

"PatB" wrote:
Just starting to move to ASP.NET 2.0 and having trouble with the Global.asax
code file.

In 1.1 I could have a code behind file for the global.asax file. This allow
for shared variables of the Global class. Note: I use these shared variables
for read only values that are set at application start.

It would appear the 2.0 doesn't like you to use shared variables in the
global class. How do I convert my 1.1 applications to 2.0 without the
ability to have these shared variables?

Why would Microsoft get rid of the global.asax code behind? There must a
reason and a good conversion method (I hope).
--
Pat B
BCC Software, Inc
A BÖWE BELL + HOWELL COMPANY
Jul 16 '07 #6

P: n/a
re:
!I implemented the work-around and it appears to be working.

Yes, it does work.

re:
!In the 2.0 code it appears that I need to reference
!then with two global references like this: Global.Global.VariableName.

!Should it be like this?

I can only suggest for you to look for a Global subclassing of Global in your code.
Experiment...and remove one of the Global subclassings.

re:
!Does anyone know the reasoning why Microsoft removed the code behind model for the global.asax?

Because it's wasted effort.
You don't need to compile the codebehind first, and then compile the global.asax class again at runtime.

Anything you can do in codebehind you can also do inline, so the codebehind isn't needed.
You can still implement it, like I pointed out, but, really, you're only duplicating compilation efforts.


Juan T. Llibre, asp.net MVP
asp.net faq : http://asp.net.do/faq/
foros de asp.net, en espaol : http://asp.net.do/foros/
======================================
"PatB" <Pa**@community.nospamwrote in message news:18**********************************@microsof t.com...
Juan,

I implemented the work-around and it appears to be working.

In my 1.1 code I can reference these shared variables by
Global.VariableName. In the 2.0 code it appears that I need to reference
then with two global references like this: Global.Global.VariableName.

Should it be like this? Is something wrong? This will mean when converting
my 1.1. apps I'll have to make a lot of changes to change the Global.
reference to Global.Global.

Does anyone know the reasoning why Microsoft removed the code behind model
for the global.asax?
--
Pat B
BCC Software, Inc
A BWE BELL + HOWELL COMPANY
"Juan T. Llibre" wrote:
>It's not the default any more, but you can still use it.

Read this article :
http://www.xerratus.com/2006/10/20/N...arateFile.aspx

You need to place all your global.asax code in global.asax.cs, in the App_Code directory

You must include this at the top of global.asax.cs :

public class Global : System.Web.HttpApplication

....and include this directive in global.asax :
<%@ Application Language="C#" CodeBehind="Global.asax.cs" Inherits="Global" %>

the same for vb :
<%@ Application Language="VB" CodeBehind="Global.asax.vb" Inherits="Global" %>

Download a complete example from :

http://www.xerratus.com/ct.ashx?id=c...iteExample.zip

Juan T. Llibre, asp.net MVP
asp.net faq : http://asp.net.do/faq/
foros de asp.net, en espaol : http://asp.net.do/foros/
======================================

"PatB" <Pa**@community.nospamwrote in message news:45**********************************@microsof t.com...
Just starting to move to ASP.NET 2.0 and having trouble with the Global.asax
code file.

In 1.1 I could have a code behind file for the global.asax file. This allow
for shared variables of the Global class. Note: I use these shared variables
for read only values that are set at application start.

It would appear the 2.0 doesn't like you to use shared variables in the
global class. How do I convert my 1.1 applications to 2.0 without the
ability to have these shared variables?

Why would Microsoft get rid of the global.asax code behind? There must a
reason and a good conversion method (I hope).
--
Pat B
BCC Software, Inc
A BWE BELL + HOWELL COMPANY



Jul 16 '07 #7

P: n/a
Bruce,

Try to add a shared variable? You can't do it without a code behind file
with a class definition. At least I couldn't get it to do it.

--
Pat B
BCC Software, Inc
A BÖWE BELL + HOWELL COMPANY
"bruce barker" wrote:
there is no need for a codebehind file. you just put the code in
global.asax file. as there is nothing but the code in the global.asax
file, 2 files don't make much sense.

-- bruce (sqlwork.com)


PatB wrote:
Just starting to move to ASP.NET 2.0 and having trouble with the Global.asax
code file.

In 1.1 I could have a code behind file for the global.asax file. This allow
for shared variables of the Global class. Note: I use these shared variables
for read only values that are set at application start.

It would appear the 2.0 doesn't like you to use shared variables in the
global class. How do I convert my 1.1 applications to 2.0 without the
ability to have these shared variables?

Why would Microsoft get rid of the global.asax code behind? There must a
reason and a good conversion method (I hope).
Jul 16 '07 #8

P: n/a
Ah, my turn to say to you : good point, Peter.
WAP can work that way.

I still prefer inline code, though. I use it when building WAP apps.

;-)

Juan T. Llibre, asp.net MVP
asp.net faq : http://asp.net.do/faq/
foros de asp.net, en espaol : http://asp.net.do/foros/
======================================
"Peter Bromberg [C# MVP]" <pb*******@yahoo.yabbadabbadoo.comwrote in message
news:2E**********************************@microsof t.com...
If you're "just starting" to move into ASP.NET 2.0, then it's not too late to
start developing good habits. One that I've gotten is NOT to use the Web Site
Project model, but the Web Application Project model for building my "stuff".
The original global.asax codebehind arrangement is preserved. My humble
opinion, of course.
-- Peter
Site: http://www.eggheadcafe.com
UnBlog: http://petesbloggerama.blogspot.com
BlogMetaFinder(BETA): http://www.blogmetafinder.com

"PatB" wrote:
>Just starting to move to ASP.NET 2.0 and having trouble with the Global.asax
code file.

In 1.1 I could have a code behind file for the global.asax file. This allow
for shared variables of the Global class. Note: I use these shared variables
for read only values that are set at application start.

It would appear the 2.0 doesn't like you to use shared variables in the
global class. How do I convert my 1.1 applications to 2.0 without the
ability to have these shared variables?

Why would Microsoft get rid of the global.asax code behind? There must a
reason and a good conversion method (I hope).
--
Pat B
BCC Software, Inc
A BWE BELL + HOWELL COMPANY

Jul 16 '07 #9

P: n/a
Peter,

I created a ASP.NET AJAX-enabled Web application.

Your comments confuse me. What's the difference between Web Site Project
model amd the Web Application Project model? How do I switch or create a Web
Application Project Model? Am I missing something during the project creation?

Thank you,

--
Pat B
BCC Software, Inc
A BÖWE BELL + HOWELL COMPANY
"Peter Bromberg [C# MVP]" wrote:
If you're "just starting" to move into ASP.NET 2.0, then it's not too late to
start developing good habits. One that I've gotten is NOT to use the Web Site
Project model, but the Web Application Project model for building my "stuff".
The original global.asax codebehind arrangement is preserved. My humble
opinion, of course.
-- Peter
Site: http://www.eggheadcafe.com
UnBlog: http://petesbloggerama.blogspot.com
BlogMetaFinder(BETA): http://www.blogmetafinder.com

"PatB" wrote:
Just starting to move to ASP.NET 2.0 and having trouble with the Global.asax
code file.

In 1.1 I could have a code behind file for the global.asax file. This allow
for shared variables of the Global class. Note: I use these shared variables
for read only values that are set at application start.

It would appear the 2.0 doesn't like you to use shared variables in the
global class. How do I convert my 1.1 applications to 2.0 without the
ability to have these shared variables?

Why would Microsoft get rid of the global.asax code behind? There must a
reason and a good conversion method (I hope).
--
Pat B
BCC Software, Inc
A BÖWE BELL + HOWELL COMPANY
Jul 16 '07 #10

P: n/a
re:
!Try to add a shared variable? You can't do it without a code behind file
!with a class definition. At least I couldn't get it to do it.

Why can't you use an Application Variable to do that ?

Juan T. Llibre, asp.net MVP
asp.net faq : http://asp.net.do/faq/
foros de asp.net, en espaol : http://asp.net.do/foros/
======================================
"PatB" <Pa**@community.nospamwrote in message news:7A**********************************@microsof t.com...
Bruce,

Try to add a shared variable? You can't do it without a code behind file
with a class definition. At least I couldn't get it to do it.

--
Pat B
BCC Software, Inc
A BWE BELL + HOWELL COMPANY
"bruce barker" wrote:
>there is no need for a codebehind file. you just put the code in
global.asax file. as there is nothing but the code in the global.asax
file, 2 files don't make much sense.

-- bruce (sqlwork.com)


PatB wrote:
Just starting to move to ASP.NET 2.0 and having trouble with the Global.asax
code file.

In 1.1 I could have a code behind file for the global.asax file. This allow
for shared variables of the Global class. Note: I use these shared variables
for read only values that are set at application start.

It would appear the 2.0 doesn't like you to use shared variables in the
global class. How do I convert my 1.1 applications to 2.0 without the
ability to have these shared variables?

Why would Microsoft get rid of the global.asax code behind? There must a
reason and a good conversion method (I hope).


Jul 16 '07 #11

P: n/a
let's deal with one issue at a time or we will all get confused!
Web Site Project - the original ASP.NET model that shipped with Visual
Studio 2005.
Web Application Project - the new model that is similar to Visual Studio
2003 (has a project file, builds a single dll assembly). This was an add-on
delivered in 2006 and is now standard as of Service Pack 1.

AJAX projects are a separate issue, but you should still be able to do the
codebehind as explained by others in this thread.
-- Peter
Site: http://www.eggheadcafe.com
UnBlog: http://petesbloggerama.blogspot.com
BlogMetaFinder(BETA): http://www.blogmetafinder.com

"PatB" wrote:
Peter,

I created a ASP.NET AJAX-enabled Web application.

Your comments confuse me. What's the difference between Web Site Project
model amd the Web Application Project model? How do I switch or create a Web
Application Project Model? Am I missing something during the project creation?

Thank you,

--
Pat B
BCC Software, Inc
A BÖWE BELL + HOWELL COMPANY
"Peter Bromberg [C# MVP]" wrote:
If you're "just starting" to move into ASP.NET 2.0, then it's not too late to
start developing good habits. One that I've gotten is NOT to use the Web Site
Project model, but the Web Application Project model for building my "stuff".
The original global.asax codebehind arrangement is preserved. My humble
opinion, of course.
-- Peter
Site: http://www.eggheadcafe.com
UnBlog: http://petesbloggerama.blogspot.com
BlogMetaFinder(BETA): http://www.blogmetafinder.com

"PatB" wrote:
Just starting to move to ASP.NET 2.0 and having trouble with the Global.asax
code file.
>
In 1.1 I could have a code behind file for the global.asax file. This allow
for shared variables of the Global class. Note: I use these shared variables
for read only values that are set at application start.
>
It would appear the 2.0 doesn't like you to use shared variables in the
global class. How do I convert my 1.1 applications to 2.0 without the
ability to have these shared variables?
>
Why would Microsoft get rid of the global.asax code behind? There must a
reason and a good conversion method (I hope).
>
>
--
Pat B
BCC Software, Inc
A BÖWE BELL + HOWELL COMPANY
Jul 16 '07 #12

P: n/a
I could use an application variable, but they are not strongly typed which
requires a conversion every time you use it.

Shared variables of the global class are typed and do not require a
conversion.

--
Pat B
BCC Software, Inc
A BÖWE BELL + HOWELL COMPANY
"Juan T. Llibre" wrote:
re:
!Try to add a shared variable? You can't do it without a code behind file
!with a class definition. At least I couldn't get it to do it.

Why can't you use an Application Variable to do that ?

Juan T. Llibre, asp.net MVP
asp.net faq : http://asp.net.do/faq/
foros de asp.net, en español : http://asp.net.do/foros/
======================================
"PatB" <Pa**@community.nospamwrote in message news:7A**********************************@microsof t.com...
Bruce,

Try to add a shared variable? You can't do it without a code behind file
with a class definition. At least I couldn't get it to do it.

--
Pat B
BCC Software, Inc
A BÖWE BELL + HOWELL COMPANY
"bruce barker" wrote:
there is no need for a codebehind file. you just put the code in
global.asax file. as there is nothing but the code in the global.asax
file, 2 files don't make much sense.

-- bruce (sqlwork.com)


PatB wrote:
Just starting to move to ASP.NET 2.0 and having trouble with the Global.asax
code file.

In 1.1 I could have a code behind file for the global.asax file. This allow
for shared variables of the Global class. Note: I use these shared variables
for read only values that are set at application start.

It would appear the 2.0 doesn't like you to use shared variables in the
global class. How do I convert my 1.1 applications to 2.0 without the
ability to have these shared variables?

Why would Microsoft get rid of the global.asax code behind? There must a
reason and a good conversion method (I hope).




Jul 16 '07 #13

P: n/a
re:
!Shared variables of the global class are typed and do not require a conversion.

To each his own, depending on his coding preferences, I guess.

I wouldn't sacrifice performance for a coding preference, though.

Even though the difference is not really critical,
compiling somthing twice doesn't appeal much to me.

;-)

Juan T. Llibre, asp.net MVP
asp.net faq : http://asp.net.do/faq/
foros de asp.net, en espaol : http://asp.net.do/foros/
======================================
"PatB" <Pa**@community.nospamwrote in message news:E0**********************************@microsof t.com...
>I could use an application variable, but they are not strongly typed which
requires a conversion every time you use it.
Shared variables of the global class are typed and do not require a conversion.

--
Pat B
BCC Software, Inc
A BWE BELL + HOWELL COMPANY
"Juan T. Llibre" wrote:
>re:
!Try to add a shared variable? You can't do it without a code behind file
!with a class definition. At least I couldn't get it to do it.

Why can't you use an Application Variable to do that ?

Juan T. Llibre, asp.net MVP
asp.net faq : http://asp.net.do/faq/
foros de asp.net, en espaol : http://asp.net.do/foros/
======================================
"PatB" <Pa**@community.nospamwrote in message news:7A**********************************@microsof t.com...
Bruce,

Try to add a shared variable? You can't do it without a code behind file
with a class definition. At least I couldn't get it to do it.

--
Pat B
BCC Software, Inc
A BWE BELL + HOWELL COMPANY
"bruce barker" wrote:

there is no need for a codebehind file. you just put the code in
global.asax file. as there is nothing but the code in the global.asax
file, 2 files don't make much sense.

-- bruce (sqlwork.com)


PatB wrote:
Just starting to move to ASP.NET 2.0 and having trouble with the Global.asax
code file.

In 1.1 I could have a code behind file for the global.asax file. This allow
for shared variables of the Global class. Note: I use these shared variables
for read only values that are set at application start.

It would appear the 2.0 doesn't like you to use shared variables in the
global class. How do I convert my 1.1 applications to 2.0 without the
ability to have these shared variables?

Why would Microsoft get rid of the global.asax code behind? There must a
reason and a good conversion method (I hope).



Jul 16 '07 #14

P: n/a
Thanks for Juan's input.

Hi Pat,

As Juan mentioned, due to the new dynamic compilation model, in ASP.NET
2.0, it prefer to use the inline code model for global.asax. However, you
can still use a codebehind class(define in App_Code dir) and inherit it in
@Application directive.

Or you can also direclty put a separate partial class file(ASP.NET 2.0
specific model) with the global.asax(named global.asax.cs). e.g.
====in global.asax===========
<%@ Application Language="C#" CodeFile="Global.asax.cs" Inherits="Global"
%>
====global.asax.cs=========
public partial class Global : HttpApplication
{
........
============================

In addition, for sharing some global variables, in ASP.NET 2.0 application,
I think you can consider using the following means instead of define them
in global class:

You can create a dedicated utility class(mark as public) and define some
static(shared) member variables in it so that other pages can access them.
How do you think?

Sincerely,

Steven Cheng

Microsoft MSDN Online Support Lead
This posting is provided "AS IS" with no warranties, and confers no rights.
Jul 17 '07 #15

P: n/a
Hi Pat,

Have you got any further idea on this? If you have any other questions,
please feel free to post here.

Sincerely,

Steven Cheng

Microsoft MSDN Online Support Lead
This posting is provided "AS IS" with no warranties, and confers no rights.

Jul 19 '07 #16

This discussion thread is closed

Replies have been disabled for this discussion.