6

Possible Duplicate:
How do you access application variables in asp.net mvc 3 razor views?

Is it possible to access application variable in controller using asp.net mvc2.0

2
  • 1
    can you be more specific Commented May 25, 2012 at 6:13
  • i have declared application variable in global.asax file now want to assign value in controller Commented May 25, 2012 at 6:14

1 Answer 1

20

For example, in global.asax:

Application["AppVar"] = "hello";

In any controller method:

string appVar = HttpContext.Application["AppVar"] as string;

Update (7/2018):
If you need to access MVC global application data from a DLL library:

using System.Web;
....
if (HttpContext.Current != null && HttpContext.Current.Application != null)
    string appVar = HttpContext.Current.Application["AppVar"] as string;

It is safer to check HttpContext.Current.Application against null as well, because some fake httpcontext library (used in unit test projects) could have a valid context with null "Application".

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.