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 8 5539
Typo: should be Global.asax obviously.
Thomas
"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
> 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
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
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
"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
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
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 This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
by: Patrick |
last post by:
Following earlier discussions about invoking a .NET class library via
..NET-COM Interop (using regasm /tlb) at...
|
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->...
|
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...
|
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...
|
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...
|
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...
|
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...
|
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...
|
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...
|
by: better678 |
last post by:
Question:
Discuss your understanding of the Java platform. Is the statement "Java is interpreted" correct?
Answer:
Java is an object-oriented...
|
by: CD Tom |
last post by:
This happens in runtime 2013 and 2016. When a report is run and then closed a toolbar shows up and the only way to get it to go away is to right...
|
by: CD Tom |
last post by:
This only shows up in access runtime. When a user select a report from my report menu when they close the report they get a menu I've called Add-ins...
|
by: Naresh1 |
last post by:
What is WebLogic Admin Training?
WebLogic Admin Training is a specialized program designed to equip individuals with the skills and knowledge...
|
by: jalbright99669 |
last post by:
Am having a bit of a time with URL Rewrite. I need to incorporate http to https redirect with a reverse proxy. I have the URL Rewrite rules made...
|
by: Matthew3360 |
last post by:
Hi there. I have been struggling to find out how to use a variable as my location in my header redirect function.
Here is my code.
...
|
by: Matthew3360 |
last post by:
Hi, I have a python app that i want to be able to get variables from a php page on my webserver. My python app is on my computer. How would I make it...
|
by: AndyPSV |
last post by:
HOW CAN I CREATE AN AI with an .executable file that would suck all files in the folder and on my computerHOW CAN I CREATE AN AI with an .executable...
|
by: Arjunsri |
last post by:
I have a Redshift database that I need to use as an import data source. I have configured the DSN connection using the server, port, database, and...
| |