Oli*_*ver 10 c# asp.net http-headers
我正在尝试为Vary: Accept-Encoding
我压缩的文件添加一个标头,如前所述.
但是,由于某种原因,无法从Visual Studio测试服务器或IIS服务器进行此操作.
我有以下代码:
if (url.Contains(".js") || url.Contains(".aspx") || url.Contains(".css"))
{
app.Response.AppendHeader("Vary", "Accept-Encoding");
app.Response.AppendHeader("Varye", "Accept-Encoding"); // for testing
app.Response.AppendHeader("Varye", "Accept-Things"); // for testing
app.Response.AppendHeader("Vary", "Accept-Stuff"); // for testing
app.Response.AppendHeader("Var", "Accept-Items"); // for testing
encodings = encodings.ToLower();
if (encodings.Contains("gzip") || encodings == "*")
{
app.Response.Filter = new GZipStream(baseStream, CompressionMode.Compress);
app.Response.AppendHeader("Content-Encoding", "gzip");
}
}
Run Code Online (Sandbox Code Playgroud)
这导致以下响应头:
Status=OK - 200
Server=ASP.NET Development Server/10.0.0.0
Date=Fri, 21 Oct 2011 12:24:11 GMT
X-AspNet-Version=4.0.30319
Varye=Accept-Encoding, Accept-Things
Var=Accept-Items
Content-Encoding=gzip
Cache-Control=public
Etag="1CC8F2E9D772300"
Content-Type=text/css
Content-Length=16200
Connection=Close
Run Code Online (Sandbox Code Playgroud)
如您所见,Vary
标题不存在.存在具有类似语法的无意义标头,因此必须有某些东西,在发送之前取出Vary标头.
我不知道它是否相关,但这里是我在web.config中定义压缩模块的地方:
<httpModules>
<add name="CompressionModule" type="Utility.HttpCompressionModule"/>
</httpModules>
Run Code Online (Sandbox Code Playgroud)
(其中Utility.HttpCompressionModule是我上面提供的代码摘录所属的类.)
为什么我不能添加Vary
标题?
编辑: Eric C的解决方案给我留下了这样的代码:
if (url.Contains(".js") || url.Contains(".aspx") || url.Contains(".css"))
{
app.Response.Cache.SetVaryByCustom("Accept-Encoding");
encodings = encodings.ToLower();
if (encodings.Contains("gzip") || encodings == "*")
{
app.Response.Filter = new GZipStream(baseStream, CompressionMode.Compress);
app.Response.AppendHeader("Content-Encoding", "gzip");
}
Run Code Online (Sandbox Code Playgroud)
但是,标题看起来像这样:
Status=OK - 200
Server=ASP.NET Development Server/10.0.0.0
Date=Mon, 24 Oct 2011 09:26:37 GMT
Content-Encoding=gzip
Cache-Control=public
Etag="1CC7A09FDE77300"
Vary=*
Content-Type=application/x-javascript
Content-Length=44447
Connection=Close
Run Code Online (Sandbox Code Playgroud)
(不知道为什么这是application/x-javascript
HTML中的设置text/javascript
,但这是无关紧要的.)
正如您所看到的,我现在有一个不同的标题,但它被设置为Vary=*
而不是Vary=Accept-Encoding
像您在压缩模块中的代码所期望的那样.
这里发生了什么?如何Vary
正确设置标头?
第二次编辑: 我要粘贴整个类的源代码.它没有比我已发布的更多,但它可能有助于准确掌握我正在做的事情:
public class HttpCompressionModule : IHttpModule
{
/// <summary>
/// Initializes a new instance of the <see cref="AjaxHttpCompressionModule"/> class.
/// </summary>
public HttpCompressionModule()
{
}
#region IHttpModule Members
/// <summary>
/// Disposes of the resources (other than memory) used by the module that implements <see cref="T:System.Web.IHttpModule"/>.
/// </summary>
void IHttpModule.Dispose()
{
}
/// <summary>
/// Initializes a module and prepares it to handle requests.
/// </summary>
/// <param name="context">An <see cref="T:System.Web.HttpApplication"/> that provides access to the methods, properties, and events common to all application objects within an ASP.NET application</param>
void IHttpModule.Init(HttpApplication context)
{
context.BeginRequest += (new EventHandler(this.context_BeginRequest));
}
#endregion
/// <summary>
/// Handles the BeginRequest event of the context control.
/// </summary>
/// <param name="sender">The source of the event.</param>
/// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
void context_BeginRequest(object sender, EventArgs e)
{
HttpApplication app = (HttpApplication)sender;
string encodings = app.Request.Headers.Get("Accept-Encoding");
Stream baseStream = app.Response.Filter;
if (string.IsNullOrEmpty(encodings))
return;
string url = app.Request.RawUrl.ToLower();
if (url.Contains(".js") || url.Contains(".css") || url.Contains("ajax.ashx"))
{
app.Response.Cache.SetVaryByCustom("Accept-Encoding");
encodings = encodings.ToLower();
if (encodings.Contains("gzip") || encodings == "*")
{
app.Response.Filter = new GZipStream(baseStream, CompressionMode.Compress);
app.Response.AppendHeader("Content-Encoding", "gzip");
}
else if (encodings.Contains("deflate"))
{
app.Response.Filter = new DeflateStream(baseStream, CompressionMode.Compress);
app.Response.AppendHeader("Content-Encoding", "deflate");
}
}
}
}
Run Code Online (Sandbox Code Playgroud)
此外,这是我的web.config文件的System.Web部分:
<system.web>
<!--<compilation debug="true"></compilation>-->
<trace enabled="true" traceMode="SortByTime"/>
<httpRuntime executionTimeout="180"/>
<globalization culture="en-GB" uiCulture="en-GB"/>
<!-- custom errors-->
<customErrors mode="Off">
</customErrors>
<!-- Membership -->
<membership defaultProvider="SqlProvider" userIsOnlineTimeWindow="15">
<providers>
<clear/>
<add name="SqlProvider" type="System.Web.Security.SqlMembershipProvider" connectionStringName="SQLServerAuth" applicationName="mycompany" minRequiredPasswordLength="4" minRequiredNonalphanumericCharacters="0" enablePasswordRetrieval="false" enablePasswordReset="true" requiresQuestionAndAnswer="false" requiresUniqueEmail="true" passwordFormat="Hashed" maxInvalidPasswordAttempts="1024"/>
</providers>
</membership>
<!-- Roles -->
<roleManager enabled="true" cacheRolesInCookie="true" defaultProvider="SqlProvider">
<providers>
<clear/>
<add connectionStringName="SQLServerAuth" applicationName="mycompany" name="SqlProvider" type="System.Web.Security.SqlRoleProvider"/>
</providers>
</roleManager>
<!-- Authentication -->
<anonymousIdentification enabled="false"/>
<authentication mode="Forms">
<forms name=".AUTH" protection="All" timeout="2" path="/">
</forms>
</authentication>
<httpModules>
<add name="CompressionModule" type="Utility.HttpCompressionModule"/>
</httpModules>
</system.web>
Run Code Online (Sandbox Code Playgroud)
没有更多的话要说.我所知道的,没有其他非标准的东西我们正在使用该网站.有任何想法吗?
Die*_*ego 10
从IIS 7.5开始,Vary标头被gzip IIS过滤器(gzip.dll)覆盖,该过滤器实现了DyanamicCompressionModule.无论ASP.NET代码中的更改如何,过滤器始终将标头设置为"Vary:Accept-Encoding".截至今天,唯一的解决方法是禁用动态内容的压缩,然后在代码中实现它.这是如何做:
从Web.config中删除以下行:
<add name="CompressionModule" type="Utility.HttpCompressionModule"/>
然后转到IIS管理控制台并确保未对动态内容启用压缩.
Global.asax.cs
方法HttpApplication.Application_BeginRequest
:手动实现压缩:
protected void Application_BeginRequest(object sender, EventArgs e) { HttpContext context = HttpContext.Current; context.Response.Filter = new GZipStream(context.Response.Filter, CompressionMode.Compress); context.Response.AppendHeader("Content-Encoding", "gzip"); context.Response.Cache.VaryByHeaders["Accept-Encoding"] = true; // We can now set additional Vary headers... context.Response.Cache.VaryByHeaders.UserAgent = true; context.Response.Cache.VaryByHeaders["X-Requested-With"] = true; }
这是最近向Microsoft报告的问题.
正如其他人提到的,这是IIS 压缩模块专门覆盖标头的一个已知问题Vary
,并已通过修补程序解决。
如果您无法确保安装了修补程序,则另一种解决方法是在 Web.config 中使用IIS URL 重写附加标头:
<configuration>
<system.webServer>
<rewrite>
<outboundRules>
<rule name="Append 'Vary: X-Requested-With' header" patternSyntax="ECMAScript">
<match serverVariable="RESPONSE_VARY" pattern=".+" />
<action type="Rewrite" value="{R:0}, X-Requested-With" replace="true" />
</rule>
<rule name="Set 'Vary: X-Requested-With' header if no others" patternSyntax="ECMAScript">
<match serverVariable="RESPONSE_VARY" pattern=".+" negate="true" />
<action type="Rewrite" value="X-Requested-With" />
</rule>
</outboundRules>
</rewrite>
</system.webServer>
</configuration>
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
6067 次 |
最近记录: |