global.asax for non-page inheriting objects

  • Thread starter Thread starter Dylan Parry
  • Start date Start date
D

Dylan Parry

Hi folks,

I'm not really sure of the terminology here, so I'll try my best.

I have been using global.asax to set up application variables that are
used within my applications, but for obvious (or not so) reasons the
application variables can only be accessed from objects that extend the
Page object.

So my question is, is there any way of storing variables (say database
connection data that I don't want hard-coded into the assembly) in an
external file like global.asax that can be accessed by any object?

If it helps to mention it, I am stilling using .net 1.1.

TIA
 
Dylan said:
Hi folks,

I'm not really sure of the terminology here, so I'll try my best.

I have been using global.asax to set up application variables that are
used within my applications, but for obvious (or not so) reasons the
application variables can only be accessed from objects that extend the
Page object.

So my question is, is there any way of storing variables (say database
connection data that I don't want hard-coded into the assembly) in an
external file like global.asax that can be accessed by any object?

If it helps to mention it, I am stilling using .net 1.1.

Web.config?
 
Hello Dylan,

Actually Application variables can be accessed by any in-process object,
i.e. any object running under the same process as the application. To store
hard coded data, the best place would be Web.config though.

HTH,
r.
 
Pondering the eternal question of "Hobnobs or Rich Tea?", Tom
Porterfield finally proclaimed:
Web.config?

Sounds like as good a place as any, but how would I go about using
web.config for this purpose? I've used it for things like password
protecting directories and changing the error reporting etc, but not for
something like this.
 
It is very easy to have objects that do not derive from Page class to use
Application state:

public class Class1
{
public string ConnectionString;
public Class1()
{
this.ConnectionString
=(string)HttpContext.Current.Application["connectionString"];

}
}

What you might do here is have an appSettings section in your web.config,
and in Application_Start in Global, set an Application["connectionString"]
variabl to this.
Peter
 
Pondering the eternal question of "Hobnobs or Rich Tea?", Peter Bromberg
[C# MVP] finally proclaimed:
=(string)HttpContext.Current.Application["connectionString"];

Oh thanks, I didn't know you could do it like that! :)
 

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

Back
Top