locked
How to do a Response.End() within _appStart.cshtml RRS feed

  • Question

  • User-1416423428 posted

    In my _appStart.cshtml, I'm reading some configuration settings from the database.

    What I want to do is if for some reason, the configuration setting record isn't found, I want to do a proper Response.End and halt execution of the program properly, rather than displaying some Abend errors.  

    Response.End();

    I get the error : The name 'Response' does not exist in the current context

    So I did this instead. 

    System.Web.HttpContext.Current.Response.End(); 

    and now my error message is Exception Details: System.NullReferenceException: Object reference not set to an instance of an object.

    Question: How can I terminate program from within _appStart.cshtml ? 

    Saturday, July 26, 2014 2:09 AM

Answers

  • User-821857111 posted

    The AppStart file is a substitute for the Application_Start event in global.asax. The Response object is not available at that point when the application is running in Integrated mode. It is available if you use classic mode. You could set an application variable to false if your config is not found, then check the value of the app variable in a PageStart file. You can redirect at that point.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Saturday, July 26, 2014 5:32 PM

All replies

  • User-821857111 posted

    What exactly do you want to happen if the config details aren't found? What do you want the user to see?

    Saturday, July 26, 2014 4:03 AM
  • User465171450 posted

    Remember, you aren't halting the execution of the program, your are just killing the repsonse object. Your pipeline will still continue to process and the next steps will fail because you have killed the response object and they can no longer perform actions against it. 

    Think what happens when you call Response.End. You aren't ending program execution. If you did, what is that going to do for a user? What will they see? Perhaps instead of attempting to end it you try to redirect any responses to a static error page letting them know what is going on. Otherwise they just go to a website that doesn't respond.

    Saturday, July 26, 2014 11:11 AM
  • User-1416423428 posted

    Ideally, I want to print a simple message saying "config not found" or something like that, and then Response.End.  

    In real-world use, this condition probably wouldn't happen (config record not found), but just want to practice defensive programming. 

    Saturday, July 26, 2014 4:44 PM
  • User-1416423428 posted

    Doing a Response.Redirect() also doesn't work within _appstart.cshtml

    System.NullReferenceException: Object reference not set to an instance of an object.

    System.Web.HttpContext.Current.Response.Redirect("/test.asp");

    Saturday, July 26, 2014 5:02 PM
  • User-821857111 posted

    The AppStart file is a substitute for the Application_Start event in global.asax. The Response object is not available at that point when the application is running in Integrated mode. It is available if you use classic mode. You could set an application variable to false if your config is not found, then check the value of the app variable in a PageStart file. You can redirect at that point.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Saturday, July 26, 2014 5:32 PM
  • User895691971 posted

    The AppStart file is a substitute for the Application_Start event in global.asax. The Response object is not available at that point when the application is running in Integrated mode. It is available if you use classic mode. You could set an application variable to false if your config is not found, then check the value of the app variable in a PageStart file. You can redirect at that point.

    Adding a line to Mikes answer, you can also add a Session variable to play with. This way each time the Session is created a variable would be stored that you can use in further executions, so that you can know _AppStart page is present. This can be done as a trick (as Mike suggested; for granted)

    // in the AppStart write this, to create a variable
    
    Session["AppStartPresent"] = "true";
    
    // in the PageStart get the value
    
    if(Session["AppStartPresent"] != null) {
       // must be checked, otherwise
       // NullReferenceException might be caught!
       // go ahead
    } else {
       Response.Redirect("~/SomeErrorPage.cshtml");
    }

    This can be used here to redirect the user in the PageStart page not in the AppStart or something.

    Sunday, July 27, 2014 6:41 AM
  • User-821857111 posted

    // in the AppStart write this, to create a variable
    
    Session["AppStartPresent"] = "true";

    You can't set Session variables in AppStart. Session is not available there. 

    Sunday, July 27, 2014 3:38 PM