1

I have been working in ASP.NET MVC for a while now, but one thing that has confused me about it is the difficulty involved in modifying HTTP Headers. It is simple enough to add headers (Response.AppendHeader(...);), and it is very (almost too) simple to remove all headers (Response.ClearHeaders();). But when it comes to modifying and deleting individual headers, there's trouble.

What is the technical reason behind the http headers collection being more than (essentially) a simple Dictionary<string, string> that gets written to the response stream? Why is it so easy to remove all, but impossible to remove an individual?

Note - I am not only concerned with running it on the server (using IIS 7, integrated pipeline mode), but also in development (using Cassini).

4

1 回答 1

0

对于 .Net Framwork 3.0、IIS 7.0/集成模式:

如您所见,

Response

对象有

Headers

键入为 NameValueCollection 的属性(我不确定为什么选择了这种类型) http://msdn.microsoft.com/en-us/library/system.web.httpresponse.headers

因此,理论上,您可以使用它在问题中概述的响应中添加或删除标题。

正如我从 Response.AddHeader 源代码中看到的(我使用了 Reflector),几乎没有额外的检查。例如,如果您在调用“Flush”方法后尝试添加处理程序,该方法将引发异常(如果您考虑一下,这是一个正确的行为)。我不确定为什么微软人员公开了 Headers 属性,以及它背后的想法是什么。我认为它至少应该受到保护,因为幕后还有其他逻辑,并且在某些情况下,该过程不限于简单的数组项插入。

于 2012-05-22T22:16:10.063 回答