473,387 Members | 1,745 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

ASP2.0 and Global.aspx

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
8 5656
Typo: should be Global.asax obviously.
Thomas
Nov 19 '05 #2

"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
> 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
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
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

"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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: Patrick | last post by:
Following earlier discussions about invoking a .NET class library via ..NET-COM Interop (using regasm /tlb) at...
13
by: Patrick | last post by:
I understand that with IIS5.1 on Windows XP Professional SP1, I can 1) Either set under IIS Manager-> Any specific Virtual Directory-> Configuration->Options->ASP Script timeout for all pages...
6
by: Prince | last post by:
I have a question about the global.asax.cs file. I'm reading info from a database to populate a DataGrid. I read somewhere that the opening of the database should occur in the global.asax.cs...
22
by: fd123456 | last post by:
Hi Tom ! Sorry about the messy quoting, Google is playing tricks on me at the moment. > Global.asax is where you normally have the Global Application > and Session variables and code to...
7
by: Ankit Aneja | last post by:
I put the code for url rewrite in my Application_BeginRequest on global.ascx some .aspx pages are in root ,some in folder named admin and some in folder named user aspx pages which are in user...
6
by: Michael Tissington | last post by:
I have setup up Forms Authentication on my website and added configuration/mappings for exe, pdf and zip files (using C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727\aspnet_isapi.dll) However when...
1
by: chris.rust | last post by:
Has anyone else had any trouble setting up a sitemap view to be filtered by roles? We've put a cust role provider in place, and we can verify that it's only being called once for each...
19
by: furiousmojo | last post by:
This is a strange problem. I have a project where the contents of global.asax application_error are not firing. It is an asp.net 2.0 application using web application projects. I have another...
1
by: יוני גולדברג | last post by:
Hi, I've developed few asp 1.1 application, now I'm about to develop my first asp2.0 appliation. I'm afraid to use old techniques so I want to focus and learn the best Asp2.0 techniques. How do...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.