title: Global.asax Error handling: Quirks of server.transfer
body: <p>In my global.asax, I am checking for a 404, and transferring to the 404 error page as per the below:</p>
  
  <pre><code>    If HTTPExceptionInstance.GetHttpCode = 404 Then
  
          Server.ClearError()
  
          Response.TrySkipIisCustomErrors = True
          Response.Status = "404 Not Found"
  
          Server.Transfer("~/Invalid-Page.aspx")
  
      End If
  </code></pre>
  
  <p>The problem is, my Invalid-page.aspx uses some <strong>session code</strong> (Session("somevariable")), which throws an exception <em>"Session state can only be used when enableSessionState is set to true, either in a configuration file or in the Page directive. "</em> because I am using a server.transfer (I believe this is a known issue?). </p>
  
  <p>If I use a Response.Redirect, there is <strong>no</strong> problem. However, this would mean that the header of the error page is a 200, not a 404. </p>
  
  <p>What would be the best workaround for this?</p>