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

一笑奈何 提交于 2019-12-01 08:03:23

问题


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:

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


回答1:


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();
                    }
                }
            }
        }



回答2:


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:


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.



来源:https://stackoverflow.com/questions/29843418/how-can-i-programmatically-stop-the-current-website

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!