1
votes

I have a hybrid (use both MVC and classic ASP pages) ASP (C#) Net Application and need to implement common error handling for both MVC and legacy codes; Namely, I have to detect invalid URLs and re-route the invalid request to either home page or login page (depending whether the user is logged in or not).

I have added the error handling code inside the 'Application_Error' (See code below).

The issue is the following: loggedin user id is kept in Session object and for some invalid URLs session object becomes null with: "session state is not available in this context"

For example:

for the following URLs, the Session object is present:

 1. http://myserver:49589/test/home/index
 2. http://myserver:49589/test/home/in
 3. http://myserver:49589/test/ho

But for the following URL, the session object is null:

 4. http://myserver:49589/te

So, the question is why session object becomes null when I misspell the folder name in the Request, and how I can solve this issue.

Routing Map is the following:

context.MapRoute(
    "default",
    "test/{controller}/{action}/{id}",
    new { action = "Index", id = UrlParameter.Optional }
);


protected void Application_Error(object sender, EventArgs e)
{
    Exception exception = Server.GetLastError();
    Response.Clear();

    HttpException httpException = exception as HttpException;
    if (httpException != null) // Http Exception
    {
        switch (httpException.GetHttpCode())
        {
            case 400: // Bad Request
            case 404: // Page Not Found
            case 500: // Internal Server Error
                {
                    // Clear the error on server.
                    Server.ClearError();

                    ServerConfiguration scfg = ServerConfiguration.Instance;
                    if (ConnxtGen.App.AppUtility.GetCurrentUserID() != -1)
                    {
                        Response.RedirectToRoute("Unity_default", new { controller = "Home", action = "Index" });
                    }
                    else
                    {
                        Response.Redirect(scfg.PagePath + "/login/login.aspx", false);
                    }
                    break;
                }
            default:
                {
                    break;
                }
        }
    }
    // Avoid IIS7 getting in the middle
    Response.TrySkipIisCustomErrors = true;
}
1

1 Answers

1
votes

One thing you should understand is that Session variables are only available after the HttpApplication.AcquireRequestState event has happened.

In your question, the moment where you want to get some information in the session is too early in the process of the Asp.Net page lifecycle.

I found this post which will surely better explain when the session object will be available:

Asp.net What to do if current session is null?

And here is a great article that explain in deeper details the entire internal process of Asp.Net page lifecycle:

ASP.NET Application and Page Life Cycle