What happened to the Global class code behing in 2.0?


G

Guest

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).
 
Ad

Advertisements

J

Juan T. Llibre

It's not the default any more, but you can still use it.

Read this article :
http://www.xerratus.com/2006/10/20/NET20GlobalasaxCodeInSeparateFile.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=...ent/binary/GlobalCodeBehindWebSiteExample.zip
 
P

Patrice

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).
 
B

bruce barker

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)
 
G

Guest

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 said:
It's not the default any more, but you can still use it.

Read this article :
http://www.xerratus.com/2006/10/20/NET20GlobalasaxCodeInSeparateFile.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=...ent/binary/GlobalCodeBehindWebSiteExample.zip
 
Ad

Advertisements

G

Guest

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.
 
J

Juan T. Llibre

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.





PatB said:
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?
 
J

Juan T. Llibre

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.

;-)
 
G

Guest

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
 
J

Juan T. Llibre

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 ?
 
Ad

Advertisements

G

Guest

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
 
G

Guest

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.
 
J

Juan T. Llibre

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.

;-)
 
S

Steven Cheng[MSFT]

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.
 
Ad

Advertisements

S

Steven Cheng[MSFT]

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.
 
Ad

Advertisements


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