使用处理程序重定向页面?

时间:2012-09-14 06:51:10

标签: c# asp.net

如果在asp.net处理程序中没有填写会话时如何重定向页面?

1 个答案:

答案 0 :(得分:0)

将以下类添加到app_code(站点根目录)

Imports Microsoft.VisualBasic

Public Class SecurityCrm
    Implements IHttpModule


Public Sub Dispose() Implements System.Web.IHttpModule.Dispose

End Sub

Public Sub Init(ByVal context As System.Web.HttpApplication) Implements System.Web.IHttpModule.Init
    AddHandler context.BeginRequest, AddressOf FirstTest
End Sub

Private Sub FirstTest(ByVal sender As Object, ByVal e As EventArgs)

    Dim app As HttpApplication
    app = CType(sender, HttpApplication)


        Dim cookie As HttpCookie ' Replace with Session Object



        cookie = app.Request.Cookies("username") ' Replace with your Session

        If cookie Is Nothing Then
            app.Response.redirect("/Cms/LoginSystem/login.asp?u=" & app.Request.Url.AbsolutePath)
            app.Response.end()
        Else
            'app.Response.write("Cookie: " & cookie.value)
        End If


End Sub

结束班

然后在web.config中注册。

  <httpModules>
    <add type="SecurityCrm" name="SecurityCrm.app_code" />
  </httpModules>