How to Access Public Properties from Global.asax

361 views Asked by At

I am new to working with the global.asax file. I have an application where I need to set some application wide variables the values for which as sourced from the web.config file. The global asax seemed to be the logical place to hold this content. My global asax file is as follows:

Imports System.Web.Http
Imports System.Web.Optimization
Imports System.Net.Http.Formatting
Imports System.Net.Http.Headers
Imports utl = Common.Utilities

 Public Class WebApiApplication
    Inherits System.Web.HttpApplication

      Private _SptZones As String
      Private _spdZones As String
      Private _spmZones As String

      Sub Application_Start()

                GlobalConfiguration.Configuration.Formatters.XmlFormatter.MediaTypeMappings.Add(New QueryStringMapping("xml", "true", "application/xml"))
                GlobalConfiguration.Configuration.Formatters.JsonFormatter.SupportedMediaTypes.Add(New MediaTypeHeaderValue("text/html"))

                AreaRegistration.RegisterAllAreas()
                GlobalConfiguration.Configure(AddressOf WebApiConfig.Register)
                FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters)
                RouteConfig.RegisterRoutes(RouteTable.Routes)
                BundleConfig.RegisterBundles(BundleTable.Bundles)

                AppInitialise()

      End Sub
      ''' <summary>
      ''' Initialise the application,  load settings from web.config file
      ''' </summary>
      Private Sub AppInitialise()
                _SptZones = utl.GetSetting("SptZones")
                _spdZones = utl.GetSetting("SpdZones")
                _spmZones = utl.GetSetting("SpmZones")

      End Sub

      Public ReadOnly Property SptZones As String
                Get
                          Return _SptZones
                End Get
      End Property
      Public ReadOnly Property SpdZones As String
                Get
                          Return _spdZones
                End Get
      End Property

      Public ReadOnly Property SpmZones As String
                Get
                          Return _spmZones
                End Get
      End Property

 End Class

I then need to access the Properties in global.asax from the Controller as in:

If scadaSource.IndexOf("SPT") > -1 Then
    'pseudo code
    criteria &= global asax property SptZones
End If
 
1

There are 1 answers

0
lorcQc On

Since these are not shared properties, they can be obtained from the current instance of HttpApplication

DirectCast(HttpContext.ApplicationInstance, WebApiApplication).SptZones