1

In my global.asax, I am checking for a 404, and transferring to the 404 error page as per the below:

    If HTTPExceptionInstance.GetHttpCode = 404 Then

        Server.ClearError()

        Response.TrySkipIisCustomErrors = True
        Response.Status = "404 Not Found"

        Server.Transfer("~/Invalid-Page.aspx")

    End If

The problem is, my Invalid-page.aspx uses some session code (Session("somevariable")), which throws an exception "Session state can only be used when enableSessionState is set to true, either in a configuration file or in the Page directive. " because I am using a server.transfer (I believe this is a known issue?).

If I use a Response.Redirect, there is no problem. However, this would mean that the header of the error page is a 200, not a 404.

What would be the best workaround for this?

Donnie Thomas
  • 3,888
  • 8
  • 47
  • 70

2 Answers2

3

Weird! Server.TransferRequest does exactly what I want without losing the session state!

Donnie Thomas
  • 3,888
  • 8
  • 47
  • 70
  • 2
    Thank you! This has been bugging me for hours. The semantic of `TransferRequest` is different than of `Transfer` (e.g. it apparently creates a new `HttpContext`, thus any `Items` in the current context are lost), but it is a good workaround for the lost session problem. – Jakub Januszkiewicz May 28 '13 at 14:40
0

Check if EnableSessionState is set to true in the page directive of Invalid.aspx

Vinay B R
  • 8,089
  • 2
  • 30
  • 45
  • I've added it, but it didn't make a difference. As I understand it, the problem seems to be caused due to server.transfer, which seems to be killing the session... – Donnie Thomas Sep 02 '10 at 15:21