Global.asax Error handling: Quirks of server.transfer

放肆的年华 提交于 2019-12-05 07:07:49

问题


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?


回答1:


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




回答2:


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



来源:https://stackoverflow.com/questions/3620530/global-asax-error-handling-quirks-of-server-transfer

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!