hankknight
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#?
You could make it const or static. Then just reference it using the class name.
e.g.
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.
e.g.
public class Global
{
public const string YOUR_GLOBAL_STRING = "the value";
...
}
public class APage : Page
{
protected Page_Load(object sender, EventArgs e)
{
string example = Global.YOUR_GLOBAL_STRING;
}
}
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.
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: e.cs code:
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>
Here is my App_Code\MyCustomHttpModulusing 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)
{
}
}
}
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);
}
}
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
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
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.