14

ASP.NET 提供的 app_offline.htm 文件返回 http 状态 503。这是大多数情况下的正确行为。但是,在请求特定 URL 的情况下(例如https://www.mywebsite.com/monitor),我想将返回的 http 状态更改为 200,而在所有其他情况下仍返回 http 状态 503。这可能吗?

我之所以要这样做是因为每当我们在我们的网站上进行定期维护时,我们都会使用 app_offline.htm 文件,但我们不希望我们的正常运行时间监控服务 (Pingdom.com) 在我们的定期维护期间报告停机时间。

我认为这必须在 IIS 级别,因为 app_offline.htm 在请求处理周期的早期就得到了服务。

4

6 回答 6

14

Note that App_Offline is only there to take the ASP.NET part down, it has nothing to do with the IIS site. All non-ASP.NET request -like .htm- will go through the normal IIS pipeline.

That being said, a HTTP 503 is an unavailable service error. The App_Offline.htm take the site partially offline, it is normal and correct that all ASP.NET request get a 503 response when the site is offline.

Bypass this with a HttpModule or whatever code in the ASP.NET pipeline is not a valid solution.

Since you'are already creating/copying the App_Offline.htm in your IIS root during maintenance, I'll suggest to add maintenance.htm as a default document for your /monitor folder or your IIS site and create/copy a maintenance.htm file in it during maintenance : then the default page will be reach whatever the ASP.NET site is offline or not.

If your probe is calling the http://servername/monitor/ uri without any page specified, it will work.

You just have to delete it -like you delete your App_Offline- after the maintenance.

于 2011-03-10T17:02:43.493 回答
4

As far as I know, the app_offline.htm logic is handled inside the ASP.NET 2.0 module but before any application loading begins (which is of course the idea behind app_offline.htm *g*).

I suggest:

  • Add a virtual directory named monitor to the root of your (disabled) Website and assign it to some IIS-readable folder.
  • Do not make the virtual directory an application, so ASP.NET should keep its hands off that folder.
  • Put e.g. a copy of your app_offline.htm file renamed to default.htm (or whatever is in your default filename list) into that folder.

This way IIS should serve the html file with a 200 response when accessing https://www.mywebsite.com/monitor.

Ah, and to honor Adilson's warning about search engines, just add a <meta name="robots" content="noindex"> to your file in case the website is reachable by search engines.

于 2011-03-09T16:28:38.103 回答
2

If you're using IIS 7 and the integrated pipeline you can still create a HttpModule for requests that are not part of the ASP.NET pipeline, i/e .Htm requests.

http://learn.iis.net/page.aspx/244/how-to-take-advantage-of-the-iis7-integrated-pipeline/

ASP.NET IIS 7 lifecycle

http://learn.iis.net/page.aspx/121/iis-7-modules-overview/

于 2011-03-13T00:15:41.243 回答
0

您可以处理 Global.asax Application_Error 方法,识别您的网址,识别您的错误,重定向到您的页面

Sub Application_Error(ByVal sender As Object, ByVal e As EventArgs)
        Dim ctx = HttpContext.Current
        dim myerror = ctx.Error
        If HttpContext.Current.Request.Path = "mypath" Then
            HttpContext.Current.Response.Redirect("~/mydestpage.aspx")
        End If
    End Sub

这将为您之前的请求返回第一个 302,然后重定向到显示 200 的页面...

于 2011-02-22T16:58:44.533 回答
0

您可以在 IIS 级别使用http 重定向。

于 2011-02-22T18:03:57.623 回答
0

嗨,老兄。这样做时要小心!一个 200 响应可以让你的错误页面被谷歌和其他搜索引擎索引。

If you still choose to do so, I think you should create a HttpModule, and put it on the top of the module stack. This module must test if the App_offline.html file exists, if so, then you test if the request came from the Site Monitor. In this case you can response with a 200, otherwise, the response code must be 503 to avoid bad site indexing.

Important: your site application pool must be in Integrated Mode.

于 2011-03-07T03:32:01.433 回答