如何以编程方式停止当前网站? [英] How can I programmatically stop the current website?
问题描述
我将 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屋!