ger
ger

Reputation: 414

access a page in the MVC Global.asax

I want to redirect to a page in the case that does not accomplish a condition in the global.asax in method application start... redirects to a warning page like this:...

protected void Application_Start()
{
    if (MyClass.Warnning == "2")
    {
       Response.Redirect("~/warningPage.aspx");
    }
}

but it shows me the following error:

response HttpException unavailable in this context...

Upvotes: 0

Views: 80

Answers (2)

As Brad mentioned, Application_Start isn't the appropriate place for a redirect since ASP is just spinning up.

One option would be to have a separate method that performs your checks and throws an exception if it does not succeed. You can then handle the exception in your Application_Error method in the Global.asax.

Upvotes: 1

Brad Christie
Brad Christie

Reputation: 101614

Application_Start isn't under the context of a request, it's the site spinning up.

You should be looking at something like Application_BeginRequest or, better yet, looking at using an ActionFilter (since you're tagging MVC).

Upvotes: 2

Related Questions