ASP.NET从Application_Start停止应用程序加载

时间:2015-05-24 13:33:10

标签: asp.net .net vb.net global-asax

有没有办法阻止ASP.NET应用程序在Application_Start事件中加载?

Sub Application_Start(sender As Object, e As EventArgs)
    Try
        Dim path As String = Server.MapPath("~/")
        Dim src As String = System.IO.File.ReadAllText(path & "src.js")
        Dim check As String = System.IO.File.ReadAllText(path & "check.js")

        If Not String.Equals(src, check, StringComparison.Ordinal) Then
            'Stop application from loading here
        End If
    Catch ex As Exception
    End Try
End Sub

2 个答案:

答案 0 :(得分:1)

如果要进行一些预检查并在检查失败时阻止应用程序启动,可以在HttpApplication的静态构造函数中抛出异常:

Sub New()
    Dim path As String = Server.MapPath("~/")
    Dim src As String = System.IO.File.ReadAllText(path & "src.js")
    Dim check As String = System.IO.File.ReadAllText(path & "check.js")

    If Not String.Equals(src, check, StringComparison.Ordinal) Then
        Throw New System.Exception("Cannot start-up application, because...")
    End If
End Sub

这将阻止应用程序被实例化,并且每个后续请求都将失败。如果检查成功,则不会重新检查每个请求。

答案 1 :(得分:0)

您可以尝试这样的事情:

Sub  Application_AuthenticateRequest(sender As Object, e As EventArgs)     
    If Not String.Equals(src, check, StringComparison.Ordinal) Then
         HttpContext.Current.Response.End();
    End If