仅当在配置文件或 Page 指令中将 enableSessionState 设置为 true 时,才能使用会话状态

发布于 2024-10-16 13:13:11 字数 1755 浏览 5 评论 0原文

我在 .net(Azure) 应用程序中收到此错误:- 会话状态只能在配置文件或页面指令中将enableSessionState设置为true时使用...

我正在重写URL(为此使用server.transfer)

当我直接访问页面或使用响应访问页面时.重定向,没有错误。

有什么想法吗?

我正在使用下面的代码。

公共类 URLRewriter 实现 IHttpModule Public Sub Init(ByVal inst As System.Web.HttpApplication) 实现 System.Web.IHttpModule.Init AddHandler inst.BeginRequest,AddressOf Me.OnBeginRequest End Sub

Public Sub OnBeginRequest(ByVal app As Object, ByVal e As EventArgs)
    Dim inst As HttpApplication = CType(app, HttpApplication)
    Dim req_path As String = inst.Context.Request.Path
    Dim trans_path As String = ""
    Select Case req_path.ToLower()
        Case "/firstpage"
            trans_path = "/Default.aspx"
        Case "/secondpage"
            trans_path = "/About.aspx"
        Case "/testapp"
            trans_path = "/Test/Test1.aspx"
        Case Else
            trans_path = "/"
    End Select

    If Not trans_path = "/" Then
        inst.Context.Server.Transfer(trans_path)
    End If
End Sub

End Class

在 web.config 中,我还在 httpModules 下添加了此标签:-

   <add name="ScriptModule" type="System.Web.Handlers.ScriptModule, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"/>
  <add name="URLRewriter" type="URLRewriter"/>
  <add type="System.Web.SessionState.SessionStateModule" name="Session" />

after adding above code in my azure application url rewrite is working fine, but when I try to use session its giving me error(Session state can only be used when enableSessionState is set to true, either in a configuration file or in the Page directive.) means its lossing the session during url rewrite.

I am getting this error in my .net(Azure) application:-
Session state can only be used when enableSessionState is set to true, either in a configuration file or in the Page directive...

I am rewritting the URL(am using server.transfer for this)

When I access the page directly or with a Response.Redirect, there is no error.

Any idea?

I am using below code.

Public Class URLRewriter
Implements IHttpModule
Public Sub Init(ByVal inst As System.Web.HttpApplication) Implements System.Web.IHttpModule.Init
AddHandler inst.BeginRequest, AddressOf Me.OnBeginRequest
End Sub

Public Sub OnBeginRequest(ByVal app As Object, ByVal e As EventArgs)
    Dim inst As HttpApplication = CType(app, HttpApplication)
    Dim req_path As String = inst.Context.Request.Path
    Dim trans_path As String = ""
    Select Case req_path.ToLower()
        Case "/firstpage"
            trans_path = "/Default.aspx"
        Case "/secondpage"
            trans_path = "/About.aspx"
        Case "/testapp"
            trans_path = "/Test/Test1.aspx"
        Case Else
            trans_path = "/"
    End Select

    If Not trans_path = "/" Then
        inst.Context.Server.Transfer(trans_path)
    End If
End Sub

End Class

In web.config I added this tag under httpModules also:-

   <add name="ScriptModule" type="System.Web.Handlers.ScriptModule, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"/>
  <add name="URLRewriter" type="URLRewriter"/>
  <add type="System.Web.SessionState.SessionStateModule" name="Session" />

after adding above code in my azure application url rewrite is working fine, but when I try to use session its giving me error(Session state can only be used when enableSessionState is set to true, either in a configuration file or in the Page directive.) means its lossing the session during url rewrite.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文