IIS7 和 HTTP 状态代码处理

发布于 2024-11-04 06:44:03 字数 1114 浏览 0 评论 0原文

我因试图对 IIS7(集成模式)中的错误呈现进行完整的编程控制而感到非常头疼。我想要做的是给出一个错误(找不到页面、内部服务器错误、未经过身份验证等),将整个请求传输到自定义 ASPX 或 HTML(我更喜欢后者),并使用正确的 HTTP 状态代码。

我想要的是 IIS7 不关心我设置的 HTTP 状态代码。我不想要它的错误处理。当我设置 Response.StatusCode = (int)HttpStatusCode.NotFound 时,我想要的不是 IIS 呈现自己的错误页面,而是可能将请求传输到另一个文件。

我已经得到了这个静态配置的东西:

<configuration>
  <system.webServer>
    <httpErrors>
      <clear />
      <error statusCode="404" path="/errors/404.html" responseMode="ExecuteURL" />
    </httpErrors>
  </system.webServer>
</configuration>

虽然这有效,但在给定错误场景的情况下,它并不能让我以编程方式控制如何处理响应。配置是一个很好的后备方案,但我真的希望能够设置 Response.StatusCode 并在某些情况下呈现与配置的 404.html 完全不同的内容(例如JSON 响应(如果我们收到 Accept: application/json),但 IIS7 不允许。没有机会。

那么我到底该怎么办呢?我尝试设置 HttpResponse.TrySkipIisCustomErrors 属性< /a>,但这看起来像是一个巨大的黑客攻击,而且似乎并不能始终如一地工作。将此属性设置为 true 真的是获得我想要的行为的推荐最佳实践吗?

此刻我的感受就是对IIS7的强烈厌恶。任何人都可以通过证明我只是愚蠢并且我确实可以完全控制 HTTP 堆栈来帮助我解决这个问题吗?

I'm having a hardcore head-ache from trying to get complete programmatic control over rendering of an error in IIS7 (integrated mode). What I want to do is given an error (page not found, internal server error, not authenticated, etc), transfer the whole request to a custom ASPX or HTML (I prefer the latter) with the correct HTTP status code.

What I want is IIS7 to don't give a crap about what I set the HTTP status code to. I don't want its error handling. When I set Response.StatusCode = (int)HttpStatusCode.NotFound, what I want is not IIS to render its own error page, but perhaps transfer the request to another file.

I've gotten this static configuration stuff to work:

<configuration>
  <system.webServer>
    <httpErrors>
      <clear />
      <error statusCode="404" path="/errors/404.html" responseMode="ExecuteURL" />
    </httpErrors>
  </system.webServer>
</configuration>

While this works, it doesn't give me programmatic control over what to do with the response, given an error scenario. Configuration is a good fallback, but I'd really like to be able to set Response.StatusCode and render something completely different from the configured 404.html in certain circumstances (like a JSON response if we receive Accept: application/json), but IIS7 won't let me. Not a chance.

So what the heck am I supposed to do? I've tried to set HttpResponse.TrySkipIisCustomErrors Property, but that looks like a huge hack and doesn't seem to work consistently. Is setting this property to true really the recommended best practice to get the behavior I want?

At the moment, the feeling I'm left with is nothing but intense hate towards IIS7. Can anyone please help me remedy this by proving that I'm just being stupid and that I can indeed have full control over the HTTP stack?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

活泼老夫 2024-11-11 06:44:03

看一下以下内容: IIS7 在设置 Response.StatusCode 时覆盖 customErrors?。

你需要设置

  <system.webServer>
    <httpErrors existingResponse="PassThrough" />
  </system.webServer>

Have a look at the following: IIS7 Overrides customErrors when setting Response.StatusCode?.

You need to set

  <system.webServer>
    <httpErrors existingResponse="PassThrough" />
  </system.webServer>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文