如何以编程方式停止当前网站? [英] How can I programmatically stop the current website?

查看:94
本文介绍了如何以编程方式停止当前网站?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在将MVC5用于Web应用程序。该Web应用程序在IIS7或更高版本中运行。

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

application_start 的Global.asax中,将设置许可证数量:

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.
    }
}

如果在这种情况下会产生任何期望,网站应该关闭,因为您可以在IIS管理器中执行以下操作:

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

如何在 Application_Start 中停止当前网站?

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

推荐答案

我不会停止它,而是在没有许可证的情况下显示一些消息。

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.

您可以在 global.asax上使用此代码。 如果您在开始时没有许可证,请打开一个标志,然后再不允许显示任何页面,然后发送可以保留html文件的页面。

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

这篇关于如何以编程方式停止当前网站?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆