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

ASP2.0 and Global.aspx

P: n/a
Ok, I've obviously discovered that Global.aspx has been completely changed in
..NET 2.0. However, I haven't figured out how to declare a constant that's
available to any page in my application without having to jump through a bunch
of hoops.

First, let me layout how it worked in 1.1. In the Global.asax, within the Global
class construct, I would simply add something like:

public const string FOO = "foo";

Now, anywhere in any page, I could do something like:
Session[Global.FOO] = "bar";

Some people have suggested using an Application level variable (or in Cache).
However, that presents it's own problem. For example, suppose we have:
Application["Foo"] = "foo";

How do I use intellisense with the indexer name? The answer is that I cannot. If
I type:
Application["Foolish"] = "foo";

That will compile and I only get an error a runtime.

So, having intellisense to global/application wide constant is a must.

Some other suggestions that I have seen:
1. Create a new base class for every page where the variable is declared.

The problem here is that my developers have to remember to inherit from this
specific base class for all pages. In addition, it complicates things when I use
master pages

2. Declare the variable in a Master page which is used by every page.

Obviously, this is similar to the above idea. The problem here is multiple
master pages complicates the issue.

3. Create a static class and add a reference every it is needed.

Yes, I could do this. But it seems highly untidy given that this is such a
common need.

4. Fiddle with HttpHanders/HttpModules.

Don't know enough about how to go about doing this for global constants. Seems
like overkill.
Declaring universally used constants is pretty common IMO. So what is the
simplest way of doing this?

Thomas


Nov 19 '05 #1
Share this Question
Share on Google+
8 Replies


P: n/a
Typo: should be Global.asax obviously.
Thomas
Nov 19 '05 #2

P: n/a

"Thomas Coleman" <th****@newsgroup.nospam> wrote in message
news:uy**************@tk2msftngp13.phx.gbl...
Ok, I've obviously discovered that Global.aspx has been completely changed
in .NET 2.0. However, I haven't figured out how to declare a constant
that's available to any page in my application without having to jump
through a bunch of hoops.

First, let me layout how it worked in 1.1. In the Global.asax, within the
Global class construct, I would simply add something like:

public const string FOO = "foo";

Now, anywhere in any page, I could do something like:
Session[Global.FOO] = "bar";
Note that Global.FOO is a constant, but Session[Global.FOO] is not a
constant. I'm not sure whether you want a global constant or a global
variable; you say the former, but your example code seems to imply the
latter.

Anyway, if you really do want a global constant, then just use Global.FOO as
you did above. That should still work in .NET 2.0.

If you want a global variable which will accurately maintain its state
across sessions, then you'll probably have to store its value in a file or
database; the Session, Application and Cache objects will lose their data
if, for example, the webserver is restarted.
3. Create a static class and add a reference every it is needed.

Yes, I could do this. But it seems highly untidy given that this is such a
common need.


This is the best solution IMHO, and I don't find it particularly untidy.
Note that it isn't the class that is static, but rather it's the field
that's static. And this is exactly what you were doing with the code above:
The constant "FOO" was a static field of the class "Global".

- Oliver
Nov 19 '05 #3

P: n/a
> Now, anywhere in any page, I could do something like:
Session[Global.FOO] = "bar";
Note that Global.FOO is a constant, but Session[Global.FOO] is not a constant.
I'm not sure whether you want a global constant or a global variable; you say
the former, but your example code seems to imply the latter.


Yes, I'm well aware that Global.FOO is a constant and that Session[Global.FOO].
I was illustrating how I might use said global constant in practice. So, this
case, I'm using as a token for a common Session object.

Anyway, if you really do want a global constant, then just use Global.FOO as
you did above. That should still work in .NET 2.0.

If you want a global variable which will accurately maintain its state across
sessions, then you'll probably have to store its value in a file or database;
the Session, Application and Cache objects will lose their data if, for
example, the webserver is restarted.


Storing it in Session, Application or Cache is not an option because it begs the
next question, "How do I formalize the tokens to pull those values?" For
example, if I have
Session["Foo"]
or Application["Foo"]
or Cache["Foo"]

I can compile the code perfectly fine with:

Session["Foobar"]

That blows my compile time checking. I want the *token* to be globally
available.

3. Create a static class and add a reference every it is needed.

Yes, I could do this. But it seems highly untidy given that this is such a
common need.


This is the best solution IMHO, and I don't find it particularly untidy. Note
that it isn't the class that is static, but rather it's the field that's
static. And this is exactly what you were doing with the code above: The
constant "FOO" was a static field of the class "Global".


Yes, I realize that "technically" only the field/property need be static even
though it is likely to make sense to make the entire class static.

The big difference betwen using the previous Global object and having to roll my
own is that it was far less code to attach global functions and constants. This
is what I call a Mickeysoft "gotcha". What was one line of code (per constant),
now becomes a new project, a reference and new DLL that has be maintained. So
much for the 70% code reduction.

Is this really the case that this simple functionality is no longer implemented?

Thomas

Nov 19 '05 #4

P: n/a
Hi Thomas,

I don't think that being able to do this sort of thing will reduce code with
70%. It's just cool to have and it's so cool I didn't know this was possbile.
That said it breaks asp.net 1 code and the people of microsoft should know
about this. If they dont than tell it to Scott Gutrie.

A class with static methods woul be my first thought to do this kind of thing
cause it has all the benefits (correct me if I'm wrong) you describe except
that you define it not in the global.asax but in a class file under the app_code
folder.

I tried some things in VS.NET and its intellisense and you still can get
to such data but not through the nice name Global.

Define this in your global.asax

public static string FOO = "foo";

This worked for me if I put it into and aspx file

Response.Write(Global_asax.FOO);

And this did the trick in the codebehind.

Response.Write(ASP.Global_asax.FOO);

Knowig this you could do a search and replace effecting lots of files.

A more transparent migration would be to define a Global class in the app_code
folder.

Cheers,
Tom Peste
Nov 19 '05 #5

P: n/a
BTW did you use the migration wizard?

While reading http://msdn.microsoft.com/asp.net/mi...dingaspnet.asp
I saw this :

<quote>
Global.Asax

In any ASP.NET application, you can use the Global.asax file to trap specific
application-level events, including startup, shutdown, session lifecycle,
request lifecycle, and error messages. This file, much like a Web service
file, has a simple shell front page (.asax) and a code-behind file.
Application changes

The conversion wizard updates your application by:
Moving the code-behind file to the App_Code directory so that it is automatically
available to any ASP.NET page in the application.
The "Code-behind" attribute is removed from the directive in the ASAX file.
(For Visual Basic) Adding a namespace statement to the class file. The namespace
is defined by the root namespace in the Web project.
</quote>

Maybe the VS wizard tries the transparent solution I mentioned in a previous
post. I haven't tested it though.

I hope it helps...

Cheers,
Tom Pester
Ok, I've obviously discovered that Global.aspx has been completely
changed in .NET 2.0. However, I haven't figured out how to declare a
constant that's available to any page in my application without having
to jump through a bunch of hoops.

First, let me layout how it worked in 1.1. In the Global.asax, within
the Global class construct, I would simply add something like:

public const string FOO = "foo";

Now, anywhere in any page, I could do something like:
Session[Global.FOO] = "bar";
Some people have suggested using an Application level variable (or in
Cache).
However, that presents it's own problem. For example, suppose we have:
Application["Foo"] = "foo";
How do I use intellisense with the indexer name? The answer is that I
cannot. If
I type:
Application["Foolish"] = "foo";
That will compile and I only get an error a runtime.

So, having intellisense to global/application wide constant is a must.

Some other suggestions that I have seen:
1. Create a new base class for every page where the variable is
declared.
The problem here is that my developers have to remember to inherit
from this specific base class for all pages. In addition, it
complicates things when I use master pages

2. Declare the variable in a Master page which is used by every page.

Obviously, this is similar to the above idea. The problem here is
multiple master pages complicates the issue.

3. Create a static class and add a reference every it is needed.

Yes, I could do this. But it seems highly untidy given that this is
such a common need.

4. Fiddle with HttpHanders/HttpModules.

Don't know enough about how to go about doing this for global
constants. Seems like overkill.

Declaring universally used constants is pretty common IMO. So what is
the simplest way of doing this?

Thomas

Nov 19 '05 #6

P: n/a

"Thomas Coleman" <th****@newsgroup.nospam> wrote in message
news:O6*************@TK2MSFTNGP15.phx.gbl...
Yes, I'm well aware that Global.FOO is a constant and that
Session[Global.FOO]. I was illustrating how I might use said global
constant in practice. So, this case, I'm using as a token for a common
Session object.
Okay, now I understand what you want. For future reference postings
though, I think you're just confusing the issue by mentioning the Session
object. If you just said you wanted a global constant, people will think you
want a global constant; if you say you want a global constant and then start
talking about global variables, people might think you made a typo or you're
confused about terminology or stuff.

Anyway, on to solve your actual problem:
The big difference betwen using the previous Global object and having to
roll my own is that it was far less code to attach global functions and
constants. This is what I call a Mickeysoft "gotcha". What was one line of
code (per constant), now becomes a new project, a reference and new DLL
that has be maintained. So much for the 70% code reduction.


I'm assuming that you're going to need these constants over multiple
projects, or else you would have made the custom global object within the
same single-project the rest of the code lies in, thereby eliminating the
"create a new project, reference and DLL" step.

If these individual projects are all part of one big web application,
then perhaps they should all be part of the same big project.

If these individual projects are really distinct applications, then
maybe they shouldn't be sharing a session object.

Without getting into the issue of whether ASP.NET sucks compared to
classic ASP or not, I'm assuming you have good reasons for sticking with
ASP.NET or else you would have switched back. Given this decision, you have
to work with the tools you've got. In this case, it sounds like you either
have to go with the unelegant solution of having yet another project, or you
should rethink your solution design (i.e. either put them all in a big
project, or don't have them share the session object).

- Oliver
Nov 19 '05 #7

P: n/a
Maybe this blogpost is helpful to you :

http://weblogs.asp.net/scottgu/archi...07/421827.aspx

Cheers,
Tom Pester
Ok, I've obviously discovered that Global.aspx has been completely
changed in .NET 2.0. However, I haven't figured out how to declare a
constant that's available to any page in my application without having
to jump through a bunch of hoops.

First, let me layout how it worked in 1.1. In the Global.asax, within
the Global class construct, I would simply add something like:

public const string FOO = "foo";

Now, anywhere in any page, I could do something like:
Session[Global.FOO] = "bar";
Some people have suggested using an Application level variable (or in
Cache).
However, that presents it's own problem. For example, suppose we have:
Application["Foo"] = "foo";
How do I use intellisense with the indexer name? The answer is that I
cannot. If
I type:
Application["Foolish"] = "foo";
That will compile and I only get an error a runtime.

So, having intellisense to global/application wide constant is a must.

Some other suggestions that I have seen:
1. Create a new base class for every page where the variable is
declared.
The problem here is that my developers have to remember to inherit
from this specific base class for all pages. In addition, it
complicates things when I use master pages

2. Declare the variable in a Master page which is used by every page.

Obviously, this is similar to the above idea. The problem here is
multiple master pages complicates the issue.

3. Create a static class and add a reference every it is needed.

Yes, I could do this. But it seems highly untidy given that this is
such a common need.

4. Fiddle with HttpHanders/HttpModules.

Don't know enough about how to go about doing this for global
constants. Seems like overkill.

Declaring universally used constants is pretty common IMO. So what is
the simplest way of doing this?

Thomas

Nov 19 '05 #8

P: n/a
The best way to declare constants that can be used by any page or class
in a project is to simply declare a class with a public const/static
member:

public class MySettings {

public const string MyStringVariable = "Some Variable";
public const int MyIntVariable = 9;
}

You can then reference these variables from any page or other class
simply as:

string foo = MySettings.MyStringVariable;
int num = MySettings.MyIntVariable;

There is no need to store this class in a separate project -- you can
just save a class in your web-project like the one above and you are
good to go. You could *optionally* store this in a helper project if
you want to share it accross multiple projects. You can use this
approach with both V1.1 and V2.0 solutions. It also works with both
Web, Windows and Class Library projects.

Note that when a V1.1 project in VS 2005 is upgraded to V2.0, the
code-behind file for the Global.asax is moved to the app_code
directory. This means that the class can actually be referenced like
before by any class or page as well -- so if you wanted to, you could
also use this approach to reference and use statics. The only thing
that might have changed which could have broken with you is that the
class name for the code-behind file might have changed (I don't have a
V1.1 project handy to test this). But the class is definitely still
there -- so if you check its name and use it that way it should work.

Hope this helps,

Scott (sc*****@microsoft.com is my email if you have other questions).

To********************@pandora.be wrote:
Maybe this blogpost is helpful to you :

http://weblogs.asp.net/scottgu/archi...07/421827.aspx

Cheers,
Tom Pester
Ok, I've obviously discovered that Global.aspx has been completely
changed in .NET 2.0. However, I haven't figured out how to declare a
constant that's available to any page in my application without having
to jump through a bunch of hoops.

First, let me layout how it worked in 1.1. In the Global.asax, within
the Global class construct, I would simply add something like:

public const string FOO = "foo";

Now, anywhere in any page, I could do something like:
Session[Global.FOO] = "bar";
Some people have suggested using an Application level variable (or in
Cache).
However, that presents it's own problem. For example, suppose we have:
Application["Foo"] = "foo";
How do I use intellisense with the indexer name? The answer is that I
cannot. If
I type:
Application["Foolish"] = "foo";
That will compile and I only get an error a runtime.

So, having intellisense to global/application wide constant is a must.

Some other suggestions that I have seen:
1. Create a new base class for every page where the variable is
declared.
The problem here is that my developers have to remember to inherit
from this specific base class for all pages. In addition, it
complicates things when I use master pages

2. Declare the variable in a Master page which is used by every page.

Obviously, this is similar to the above idea. The problem here is
multiple master pages complicates the issue.

3. Create a static class and add a reference every it is needed.

Yes, I could do this. But it seems highly untidy given that this is
such a common need.

4. Fiddle with HttpHanders/HttpModules.

Don't know enough about how to go about doing this for global
constants. Seems like overkill.

Declaring universally used constants is pretty common IMO. So what is
the simplest way of doing this?

Thomas


Nov 19 '05 #9

This discussion thread is closed

Replies have been disabled for this discussion.