Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 974
  • Last Modified:

ASP.net "Object reference not set to an instance of an object"

I know this is an age old problem, but I am having trouble with some legacy WebForms ASP.Net stuff that I must support.  The code is well written, but the developer never took into account the predictable loss of session state after the default 20 minute timeout.  

I know I can bump the timeout up a bit, but that is not the best solution.  I just want the code to sense an error (globally) and reroute itself to the home page and throw a simple error such as, "Your session timed out".

I have other web sites that do just this. But what do I need to implement to make this happen?  I don't want to go to each page of legacy code to check for a session variable.  

Thanks in advance.
Keith
0
rowek
Asked:
rowek
  • 4
  • 2
1 Solution
 
Kyle AbrahamsSenior .Net DeveloperCommented:
You can redirect to a page using the global.asax:
/// <summary>
    /// The event occurs just after Initialization of Session, and before Page_Init event
    /// </summary>
    protected void Application_PreRequestHandlerExecute(Object sender, EventArgs e)
    {
        // here it checks if session is required, as
        // .aspx requires session, and session should be available there
        // .jpg, or .css does require session so session will be null
        // as .jpg, or .css are also http request in either case if you implemented URL Rewritter, or custom IHttp Module
        if (Context.Handler is IRequiresSessionState || Context.Handler is IReadOnlySessionState)
        {
            // here is your actual code
            // check if session is new one
            // or any of your logic
            if (Session.IsNewSession                || Session.Count < 1)
            {
                // checking if request is not for default.aspx page, as it should not be redirected
                if (!Context.Request.Url.AbsoluteUri.ToLower().Contains("/default.aspx"))
                {
                    Context.Response.Redirect("~/default.aspx");
                }
            }
        }
    }

Open in new window

0
 
rowekAuthor Commented:
Kyle, just want to make sure I understand the code.  There must be an event called Application_PreRequestHandlerExecute.  Your documentation says "The event occurs just after Initialization of Session".  Is this a sure fire way to capture the timeout?  I will try to implement.  

It should not matter that the legacy app (ASP.NET 2.0) is written in VB.NET, does it?  It has been upgraded to .NET 4.0.  We had to stick with 4.0 because it runs on a 2003 Server.
0
 
rowekAuthor Commented:
Kyle, the event never fires.  The solution seems very elegant, but I don't get my event.  Here is my converted code:

    Sub Application_PreRequestHandlerExecutive(ByVal sender As Object, ByVal e As EventArgs)
        ' Code that runs just after Initialization of Session and before Page_Init event
   
        If TypeOf Context.Handler Is IRequiresSessionState OrElse TypeOf Context.Handler Is IReadOnlySessionState Then
            ' check if session is new one
            ' or any of your logic
            If Session.IsNewSession OrElse Session.Count < 1 Then
                ' checking if request is not for default.aspx page, as it should not be redirected
                If Not Context.Request.Url.AbsoluteUri.ToLower().Contains("/default.aspx") Then
                    Context.Response.Redirect("~/default.aspx")
                End If
            End If
        End If
       
       
    End Sub
0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
rowekAuthor Commented:
Kyle, you are gonna LOVE this discovery.  My C# to VB.net converter changed
Application_PreRequestHandlerExecute
to
Application_PreRequestHandlerExecutive

I cannot make this stuff up!  Once I renamed my event procedure back to the name you provided it worked great!  Thank you very much.  

Keith
0
 
rowekAuthor Commented:
Kyle's code was perfect.  My language converter played games with me, but once I found the issue the code worked great.  Believe it or not, I have been coding ASP.Net web sites since 1.0 and never found a solution to this common problem as elegant as this one.  Great job, Kyle.
0
 
Kyle AbrahamsSenior .Net DeveloperCommented:
Sorry for not responding earlier.  Glad you were able to work through it.  In the future you should tag VB as asp.net can be both VB and C#.  I program mostly in C# these days which is why you got that solution.

A good online converter:
http://www.developerfusion.com/tools/convert/csharp-to-vb/
0

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 4
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now