Link to home
Start Free TrialLog in
Avatar of hankknight
hankknightFlag for Canada

asked on

ASP.NET/C# - Define string in Global.asax

Is there a way to define a string in Global.asax that can be accessed by other ASP.NET pages using C#?
Avatar of Shaun Kline
Shaun Kline
Flag of United States of America image

You can add a string to the Application state (similar to Session state or view state, but for the entire application) on application start up method.

Take care to only add a string value that the enter application needs, and not that one session or one page needs or will change as this can cause issues.
You could make it const or static. Then just reference it using the class name.

e.g.

public class Global
{
    public const string YOUR_GLOBAL_STRING = "the value";
    ...
}

Open in new window


public class APage : Page
{
    protected Page_Load(object sender, EventArgs e)
    {
        string example = Global.YOUR_GLOBAL_STRING;
    }
}

Open in new window


Be aware that if you make it static and do not make it readonly, then any class in your application could modify its value. You may or may not desire this behavior.
Avatar of hankknight

ASKER

Thanks.  I get an error:
Compiler Error Message: CS0103: The name 'Global' does not exist in the current context

What am I doing wrong?  

Here is my Global.asax code:
<%@ Application Language="C#" %>
<%@ Import Namespace="System.Web.Routing" %>
<script runat="server">

    public class Global
    {
       public const string xPass = "hello world";
    }

    void Application_Start(object sender, EventArgs e) 
    {
        // Code that runs on application startup
        RegisterRoutes(RouteTable.Routes);
    }
    
    void Application_End(object sender, EventArgs e) 
    {
        //  Code that runs on application shutdown

    }
        
    void Application_Error(object sender, EventArgs e) 
    { 
        // Code that runs when an unhandled error occurs
    }

    void Session_Start(object sender, EventArgs e) 
    {
        // Code that runs when a new session is started
    }

    void Session_End(object sender, EventArgs e) 
    {
        // Code that runs when a session ends. 

    }
    public static void RegisterRoutes(RouteCollection routeCollection)
    {

    }
</script>

Open in new window

Here is my App_Code\MyCustomHttpModule.cs code:
using System;
using System.Collections;
using System.Collections.Generic;
using System.Data;
using System.Diagnostics;
using System.Web;
using System.IO;
using System.Text.RegularExpressions;

namespace org.MyCustomHttpModule
{
    public class CustomHeaderModule : IHttpModule
    {
        public void Dispose()
        {
        }
        public void Init(HttpApplication context)
        {
            context.BeginRequest  += OnPreSendRequestHeaders;
            context.EndRequest += new EventHandler(Application_EndRequest);

        }
        private void OnPreSendRequestHeaders(object sender, EventArgs e)
        {
             if (1==1) {
                Global.xPass = "Hello Mars";
             }
             else {
                Global.xPass = "Hello Venus";
             }
        }
        private void Application_EndRequest(object sender, EventArgs e)
        {

        }

    }
}

Open in new window

Here is my page code:
using System;
using System.Web;
using System.IO;
using System.Net;
 
public partial class abc : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        Response.Write(Global.xPass);
    }
}

Open in new window

I am used to working with the code-behind file, and that is where I had anticipated the code I demonstrated be placed. Let me see if I can determine how it would work within the markup file.
ASKER CERTIFIED SOLUTION
Avatar of kaufmed
kaufmed
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial