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#?
LVL 16
hankknightAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Shaun KlineLead Software EngineerCommented:
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.
0
käµfm³d 👽Commented:
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.
0
hankknightAuthor Commented:
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

0
käµfm³d 👽Commented:
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.
0
käµfm³d 👽Commented:
OK, within a WebForms project in VS 2012, the following is what works for me (trimmed down to the relevant bits):

Global.asax Markup
<%@ Application Codebehind="Global.asax.cs" Inherits="WebApplication13.Global" Language="C#" %>


<script RunAt="server">
    public const string xPass = "hello world!";
</script>

Open in new window


Some Page
<%@ Page Title="Home Page" Language="C#" MasterPageFile="~/Site.Master" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="WebApplication13._Default" %>

<asp:Content runat="server" ID="BodyContent" ContentPlaceHolderID="MainContent">
    <%= global_asax.xPass %>
</asp:Content>

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C#

From novice to tech pro — start learning today.