IIS7和HTTP状态代码处理 [英] IIS7 and HTTP status code handling
问题描述
我在尝试对IIS7(集成模式)中的错误渲染进行完全编程控制时遇到了麻烦。我想做的是给出错误(找不到页面,内部服务器错误,未经过身份验证等),使用正确的HTTP状态代码将整个请求传输到自定义ASPX或HTML(我更喜欢后者)。
我想要的是IIS7没有给出我设置HTTP状态代码的废话。我不想要它的错误处理。当我设置 Response.StatusCode =(int)HttpStatusCode.NotFound
时,我想要的不是IIS来呈现自己的错误页面,而是可能将请求转移到另一个文件。 / p>
我已经让这个静态配置工作了:
< ;结构>
< system.webServer>
< httpErrors>
< clear />
< error statusCode =404path =/ errors / 404.htmlresponseMode =ExecuteURL/>
< / httpErrors>
< /system.webServer>
< / configuration>
虽然这有效,但它不能让我以编程方式控制如何处理响应一个错误的场景。配置是一个很好的后备,但我真的希望能够设置 Response.StatusCode
并呈现与配置的 404.html完全不同的东西
在某些情况下(如果我们收到接受:application / json
,则为JSON响应),但IIS7不会让我。没有机会。
那我该怎么办?我试图设置 HttpResponse.TrySkipIisCustomErrors Property
,但这看起来像一个巨大的黑客,似乎并不一致。将此属性设置为true确实是建议的最佳做法,以获得我想要的行为吗?
此刻,我留下的感觉只是强烈的仇恨IIS7。任何人都可以通过证明我只是愚蠢并且我确实可以完全控制HTTP堆栈来帮助我解决这个问题吗?
请查看以下内容: IIS7在设置响应时覆盖customErrors。 StatusCode?a>。
您需要设置
< system.webServer>
< httpErrors existingResponse =PassThrough/>
< /system.webServer>
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?
Have a look at the following: IIS7 Overrides customErrors when setting Response.StatusCode?.
You need to set
<system.webServer>
<httpErrors existingResponse="PassThrough" />
</system.webServer>
这篇关于IIS7和HTTP状态代码处理的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!