Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I programmatically stop the current website? [duplicate]

I am using the MVC5 for an web application. The web app runs in IIS7 or greater.

In the Global.asax on application_start, the number of licenses will be set:

protected void Application_Start()
{
    try
    {
        MyApp.cNumberOfLicenses = COM.GetNumberOfLicenses();
    }
    catch(Exception e)
    {
        // log exception
        // stop web site.
    }
}

If any expection will be thrown in this context, the web site should shut down as you can do that in the IIS-Manager:

enter image description here

How can I stop the current web site in my Application_Start ?

like image 897
Simon Avatar asked Aug 22 '26 11:08

Simon


1 Answers

I will go not with stop it, but to show some message if you do not have license.

This is an example, and an idea.

You can use this code on global.asax where if you do not have licenses the moment you start, you open a flag, and after that you do not allow any page to show, and you send a page that you can keep on an html file.

private static bool fGotLicense = true;

protected void Application_Start()
{
    try
    {
        MyApp.cNumberOfLicenses = COM.GetNumberOfLicenses();
    }
    catch(Exception e)
    {
        // log exception
        // stop web site.
        fGotLicense = false;
    }
}

protected void Application_BeginRequest(Object sender, EventArgs e)
{
    HttpApplication app = (HttpApplication)sender;

    // if not have license - let show some infos
    if (!fGotLicens)
    {
        // the file we look now is the app_offline_alt.htm
        string cOffLineFile = HttpRuntime.AppDomainAppPath + "app_offline_alt.htm";

        // if exist on root
        if (System.IO.File.Exists(cOffLineFile))
        {
            using (var fp = System.IO.File.OpenText(cOffLineFile))
            {
                // read it and send it to the browser
                app.Response.Write(fp.ReadToEnd());
                fp.Close();
            }    
        }

       // and stop the rest of processing
       app.Response.End();
       return;
    }
}
like image 161
Aristos Avatar answered Aug 24 '26 07:08

Aristos



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!