how to expose a variable with global.asax?

  • Thread starter Thread starter D.
  • Start date Start date
D

D.

Hi,

I need to expose a variable to every aspx file of the applicationroot.
I tried with global.asax like this:
<%@ Application Language="VB" %>
<script runat="server">
public a as string
Sub Application_Start(ByVal sender As Object, ByVal e As EventArgs)
a="ok"
End Sub
</script>

When running an aspx-file in the applicationroot with this code:
<%@ Page Language="VB" %>
<%@ import namespace ="System.Web.UI.Page" %>
<% Response.Write(a)%>

i get the error: "Name 'a' is not declared"

Thanks for help
Dominique
 
Since ur hooking into Application_Start, I'll assume this is a shared
variable for all users of your site. If so, the simplest solution is to
store it in the application object:
Application.Add("SomeKey", "ok")

which you can then retrieve via:
cstr(Application("SomeKey"))


If this is a per-user/request, hook into the Application's BeginRequest and
store it in the Context.Items collection..

Karl
 
My personal preference would be to create my own class (possibly exposing
Items in a strongly type manner as explained by Karl).

This way you don't have to cast here an there from the exposed application
object plus it's likely not related to the application in the IIS sense but
ather from business point fo view...
 
Karl,

I tried what you proposed and it works.
But i also tried with cookies.
What would you advice to use?
Thanks



"Karl Seguin [MVP]" <karl REMOVE @ REMOVE openmymind REMOVEMETOO . ANDME
net> wrote in message news:[email protected]...
 
Back
Top