0

Is there any way to prevent an ASP.NET application from loading within the Application_Start event?

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
wayofthefuture
  • 8,339
  • 7
  • 36
  • 53

2 Answers2

1

If you want to do some pre-checks and prevent the application from starting if the check fails, you can throw an exception in a static constructor of the 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

This will prevent the application from being instantiated, and every following request will fail. If the check succeeds, it will not re-check on every request.

djk
  • 943
  • 2
  • 9
  • 27
0

You can try something like this:

Sub  Application_AuthenticateRequest(sender As Object, e As EventArgs)     
    If Not String.Equals(src, check, StringComparison.Ordinal) Then
         HttpContext.Current.Response.End();
    End If
Rudy
  • 2,323
  • 1
  • 21
  • 23
  • Looks like that fires on every page request, I guess I would have to store those two strings in two HttpRuntime.Cache variables maybe in order to access them without hitting the file system every page load? – wayofthefuture May 24 '15 at 13:55
  • @Dude2TheN Yes, you're right, but that is not the scope of the answer. I really don't know what are you trying to achieve. Maybe some kind of custom authentication? `HttpContext.Current.Response.End();` is what you are asking for. `Application_Start` will run only once when the web app is loaded in IIS, that is why I used `Application_AuthenticateRequest`. – Rudy May 24 '15 at 14:47