How can I programmatically stop the current websit

2020-03-27 11:20发布

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 ?

3条回答
地球回转人心会变
2楼-- · 2020-03-27 11:28

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;
    }
}
查看更多
男人必须洒脱
3楼-- · 2020-03-27 11:29

You can have a file named app_offline.htm with content say This website is offline now in web server and copy that to root directoy of website you want for any event.

It will directly show that message, but yes, App pool will be still ON, when you need to start , you just need to rename that to something else.

查看更多
叼着烟拽天下
4楼-- · 2020-03-27 11:35

You can do it with the help of "Microsoft.Web.Administration.dll"

using Microsoft.Web.Administration;

After adding the reference of "Microsoft.Web.Administration.dll" write below code in Global.asax

  protected void Application_Start(object sender, EventArgs e)
        {
            try
            {
                MyApp.cNumberOfLicenses = COM.GetNumberOfLicenses();
            }
            catch (Exception e)
            {
                // get the web site name
                var lWebSiteName = System.Web.Hosting.HostingEnvironment.ApplicationHost.GetSiteName();

                // log exception
                // stop web site.
                using (ServerManager smg = new ServerManager())
                {
                    var site = smg.Sites.FirstOrDefault(s => s.Name == lWebSiteName);
                    if (site != null)
                    {
                        //stop the site...
                        site.Stop();
                    }
                }
            }
        }
查看更多
登录 后发表回答