Franva
Franva

Reputation: 7077

Application object cannot be used in ASP.NET web page

How to use Application object in the web page?

I thought it should be something like Session object. But when I use Application, it shows the Reference like

System.Net.Mime.MediaTypeNames.Application

Obviously, it's not the one I'm looking for.

Has it been discarded in .NET 4?

If yes, what should I use to replace the Application object.

Upvotes: 0

Views: 193

Answers (4)

Tim Schmelter
Tim Schmelter

Reputation: 460158

It should work if you try to access it from an ASP.NET page. Application is a property of Page that returns the HttpApplicationState.

protected void Page_Load(object sender, EventArgs e)
{
    if(Page.Application["Foo"] != null)
    {
        // ... 
    }
}

If you want to access it from a static context, you can use HttpContext.Current:

if (HttpContext.Current.Application["Foo"] != null){ }

Upvotes: 0

nunespascal
nunespascal

Reputation: 17724

The Application (HttpApplicationState) property is very much there.

Seems you have some references that are causing the confusion.

In your CS code on a Page you should be able to use it

this.Application["key"] = myObject;

Upvotes: 0

Adriaan Stander
Adriaan Stander

Reputation: 166406

Are you referring to this one

Page.Application Property

Gets the HttpApplicationState object for the current Web request.

Upvotes: 1

Darin Dimitrov
Darin Dimitrov

Reputation: 1038890

<% 
    this.Application["test"] = "some value";
%>

inside a WebForm should work. And in the code behind it's the same story:

public partial class WebForm1 : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        this.Application["test"] = "some value";
    }
}

Upvotes: 1

Related Questions