每次我必须使用IIS7为ASP.NET添加处理程序或模块时,指令总是告诉我将它合并为两个部分:system.web和system.webserver.
<system.web>
<httpHandlers>
</httpHandlers>
<httpModules>
</httpModules>
</system.web>
Run Code Online (Sandbox Code Playgroud)
还有这个:
<system.webServer>
<modules>
</modules>
<handlers>
</handlers>
</system.webServer>
Run Code Online (Sandbox Code Playgroud)
这两个部分有什么区别?
此外,如果我不将它添加到该system.web部分,我的Visual Studio 2008调试器也无法正常工作.
我试图支持IIS下的静态文件的GZip压缩(默认情况下应该启用但不能)但到目前为止还没有工作.这是<system.webServer>Web应用程序的web.config文件中节点下的部分;
<httpCompression directory="%SystemDrive%\inetpub\temp\IIS Temporary Compressed Files">
<scheme name="gzip" dll="%Windir%\system32\inetsrv\gzip.dll" staticCompressionLevel="9" />
<dynamicTypes>
<add mimeType="text/*" enabled="true" />
<add mimeType="message/*" enabled="true" />
<add mimeType="application/x-javascript" enabled="true" />
<add mimeType="application/json" enabled="true" />
<add mimeType="*/*" enabled="false" />
</dynamicTypes>
<staticTypes>
<add mimeType="text/*" enabled="true" />
<add mimeType="message/*" enabled="true" />
<add mimeType="application/x-javascript" enabled="true" />
<add mimeType="application/atom+xml" enabled="true" />
<add mimeType="application/xaml+xml" enabled="true" />
<add mimeType="*/*" enabled="false" />
</staticTypes>
</httpCompression>
<urlCompression doStaticCompression="true" />
Run Code Online (Sandbox Code Playgroud)
我尝试使用谷歌浏览器.这是请求标题;
接受:text/html,application/xhtml + xml,application/xml; q = 0.9,/ ; q = 0.8
接收字符集:ISO-8859-1,utf-8; Q = …
我有一个WebAPI控制器返回一个HttpResponseMessage,我想添加gzip压缩.这是服务器代码:
using System.Net.Http;
using System.Web.Http;
using System.Web;
using System.IO.Compression;
[Route("SomeRoute")]
public HttpResponseMessage Post([FromBody] string value)
{
HttpContext context = HttpContext.Current;
context.Response.Filter = new GZipStream(context.Response.Filter, CompressionMode.Compress);
HttpContext.Current.Response.AppendHeader("Content-encoding", "gzip");
HttpContext.Current.Response.Cache.VaryByHeaders["Accept-encoding"] = true;
return new SomeClass().SomeRequest(value);
}
Run Code Online (Sandbox Code Playgroud)
这是使用jquery的ajax调用的客户端代码:
$.ajax({
url: "/SomeRoute",
type: "POST",
cache: "false",
data: SomeData,
beforeSend: function (jqXHR) { jqXHR.setRequestHeader('Accept-Encoding', 'gzip'); },
success: function(msg) { ... }
Run Code Online (Sandbox Code Playgroud)
当我运行它时,服务器代码返回没有错误,但客户端错误:
(failed)
net::ERR_CONTENT_DECODING_FAILED
Run Code Online (Sandbox Code Playgroud)

当我和Fiddler一起看时,这就是我所看到的:

我需要更改什么才能使Web服务返回客户端正常处理的gzip压缩内容?我知道我也可以使用HttpModule或通过IIS上的某些设置来执行此操作,但这两个选项都不适合托管的情况:

请注意,我不是在寻找IIS设置,因为我无权访问(托管).
我添加了web.config条目以启用基于此S/O答案启用IIS7 gzip的 gzip压缩.
然后我在加载ASPX页面时检查了Chrome Developer窗口,并在响应中看到了标题:
Cache-Control:private
Content-Encoding:gzip
Content-Length:3669
Content-Type:text/html; charset=utf-8
Date:Wed, 04 Mar 2015 00:46:05 GMT
Server:Microsoft-IIS/7.5
Vary:Accept-Encoding
X-AspNet-Version:4.0.30319
X-Powered-By:ASP.NET
Run Code Online (Sandbox Code Playgroud)
这意味着它"正常",对吗?但是在进行Web API调用时查找该标头时,它不存在:
Cache-Control:no-cache
Content-Length:551
Content-Type:application/json; charset=utf-8
Date:Wed, 04 Mar 2015 00:53:05 GMT
Expires:-1
Pragma:no-cache
Server:Microsoft-IIS/7.5
X-AspNet-Version:4.0.30319
X-Powered-By:ASP.NET
Run Code Online (Sandbox Code Playgroud)
我尝试了各种不同的配置(从上面链接的S/O答案中推荐的配置开始).最后,在绝望的行为中,我将其设置为此,我认为它会尝试压缩所有请求(除了*/*注释掉的所有内容):
<httpCompression directory="%SystemDrive%\inetpub\temp\IIS Temporary Compressed Files">
<scheme name="gzip" dll="%Windir%\system32\inetsrv\gzip.dll"/>
<dynamicTypes>
<add mimeType="*/*" enabled="true"/>
<!--<add mimeType="text/*" enabled="true"/>
<add mimeType="message/*" enabled="true"/>
<add mimeType="application/javascript" enabled="true"/>
<add mimeType="application/json" enabled="true"/>-->
<!--<add mimeType="*/*" enabled="false"/> -->
</dynamicTypes>
<staticTypes>
<add mimeType="*/*" enabled="true"/>
<!--<add mimeType="text/*" enabled="true"/>
<add …Run Code Online (Sandbox Code Playgroud) 是否可以使用appcmd来更改allowDefinition的值?具体来说,我尝试在应用程序级别启用对httpCompression模块的更改.
通过手动更改以下行来修改applicationHost.config:
<section name="httpCompression" allowDefinition="AppHostOnly" overrideModeDefault="Deny" />
Run Code Online (Sandbox Code Playgroud)
至
<section name="httpCompression" allowDefinition="MachineToApplication" overrideModeDefault="Allow" />
Run Code Online (Sandbox Code Playgroud)
然后允许我执行以下appcmd命令:
appcmd set config "website name" /section:httpCompression /noCompressionForProxies:false
appcmd set config "website name" /section:httpCompression /noCompressionForHttp10:false
Run Code Online (Sandbox Code Playgroud)
但是,我需要一个不依赖于手动编辑applicationHost.config的解决方案
我正在开发一个基于网络的口袋妖怪在线游戏.由于它是在线的,我想优化它以尽快运行.
我安装了Firebug,Page Speed建议缩小我的HTML输出.我也使用VS2008,ASP.NET 3.5,AJAX和IIS 7.5; 以及URL重写.
我想缩小我的HTML,JavaScript和CSS.最理想的是,我希望缩小过程在编译时发生.我花了几个小时在网上看,但找不到合适的解决方案,你能帮帮我吗?谢谢.
我已经实现了一个动作方法来缩小HTML,它提供了异常"不允许过滤"我已经搜索了互联网,但找不到任何合适的解决方案.请指导我这个问题将如何解决.我正在分享我的代码:
MinifyAttribute类:
public class MinifyAttribute : ActionFilterAttribute
{
public override void OnActionExecuting(ActionExecutingContext filterContext)
{
var request = filterContext.HttpContext.Request;
var response = filterContext.HttpContext.Response;
response.Filter = new Minify(response.Filter, s =>
{
s = Regex.Replace(s, @"\s+", " ");
s = Regex.Replace(s, @"\s*\n\s*", "\n");
s = Regex.Replace(s, @"\s*\>\s*\<\s*", "><");
s = Regex.Replace(s, @"<!--(.*?)-->", ""); //Remove comments
var firstEndBracketPosition = s.IndexOf(">");
if (firstEndBracketPosition >= 0)
{
s = s.Remove(firstEndBracketPosition, 1);
s = s.Insert(firstEndBracketPosition, ">");
}
return s;
}); // i'm getting exception here on this …Run Code Online (Sandbox Code Playgroud) 在Firebug中,请求标头具有以下条目:
Accept-Encoding:gzip,deflate
但是没有:
Content-Encoding:gzip
在Response Header中.
无论我尝试过什么,在SO和其他网站上的一些答案后,似乎没有任何作用!静态文件或动态文件都没有被压缩,或者至少如果它们没有内容编码 - gzip值会在响应头中返回.
以下是我的web.config设置示例:
<urlCompression doDynamicCompression="true" doStaticCompression="true" dynamicCompressionBeforeCache="true" />
<httpCompression directory="%SystemDrive%\inetpub\temp\IIS Temporary Compressed Files" minFileSizeForComp="150" staticCompressionIgnoreHitFrequency="true">
<remove name="gzip" />
<scheme name="gzip" dll="%Windir%\system32\inetsrv\gzip.dll" staticCompressionLevel="8" dynamicCompressionLevel="8" />
</httpCompression>
Run Code Online (Sandbox Code Playgroud)
我忽略了命中频率
staticCompressionIgnoreHitFrequency="true"
我已经确认IIS实际上压缩了我可以看到的文件:
C:\ inetpub\temp\IIS临时压缩文件
如此处指定:在IIS 8 Windows 8中设置gzip
我已确保在Windows功能> Internet信息服务> WWW服务>性能功能中启用了静态和动态压缩
我也试过这个人的方法:
IIS 7.5压缩创建压缩文件但返回非压缩文件
编辑1:
IIS版本是10但我也在IIS 8.5上尝试过这个
编辑2:
我现在也尝试了在这个链接上找到的各种配置文件:https:
//github.com/h5bp/server-configs-iis/,它提供了一些看起来像'最佳实践'的web.config文件.
没解决
编辑3:
基于@Nkosi的输入,我创建了一个全新的Asp.net MVC应用程序,并使用我尝试的所有这些选项对其进行配置.这是我从Fiddler得到的原始标题:
HTTP/1.1 200 OK
Transfer-Encoding: chunked
Content-Type: text/javascript; charset=UTF-8
Expires: Wed, 20 Jul 2016 18:22:47 GMT
Last-Modified: Wed, 20 …Run Code Online (Sandbox Code Playgroud) 我想为使用IIS7.5在ASP.NET4.5上运行的站点启用gzip压缩,但是无法让它压缩.
我在共享主机上,所以我不能直接在IIS中设置它.
的applicationHost.config
我将其更改Deny为Allow(我在此处读到我不应该更改allowDefinition设置:如何在IIS 7中使用appcmd更改allowDefinition节属性?)
<section name="httpCompression" allowDefinition="AppHostOnly" overrideModeDefault="Allow" />
Run Code Online (Sandbox Code Playgroud)
我的网站的web.config
<httpCompression directory="%SystemDrive%\inetpub\temp\IIS Temporary Compressed Files">
<scheme name="gzip" dll="%Windir%\system32\inetsrv\gzip.dll"/>
<dynamicTypes>
<add mimeType="text/*" enabled="true"/>
<add mimeType="message/*" enabled="true"/>
<add mimeType="application/javascript" enabled="true"/>
<add mimeType="application/x-javascript" enabled="true"/>
<add mimeType="application/javascript; charset=utf-8" enabled="true"/>
<add mimeType="*/*" enabled="false"/>
</dynamicTypes>
<staticTypes>
<add mimeType="text/*" enabled="true"/>
<add mimeType="message/*" enabled="true"/>
<add mimeType="application/javascript" enabled="true"/>
<add mimeType="application/x-javascript" enabled="true"/>
<add mimeType="application/javascript; charset=utf-8" enabled="true"/>
<add mimeType="*/*" enabled="false"/>
</staticTypes>
</httpCompression>
Run Code Online (Sandbox Code Playgroud)
作为上述的替代,我也尝试将其添加到我的web.config:
<configuration>
<system.webServer>
<urlCompression doStaticCompression="true" doDynamicCompression="false" />
</system.webServer>
</configuration>
Run Code Online (Sandbox Code Playgroud)
我在Windows …
我使用此答案中指定的配置设置在IIS中启用了gzip压缩.
当我使用Firefox请求文本文件时,我可以在Firebug中看到响应是gzip压缩的:

但是当我使用IE9请求相同的文本文件时,开发工具显示它不是 gzipped:

为什么IIS不发送压缩的txt文件?我错过了一些IIS设置吗?我的浏览器配置错误了吗?
我想使用GZIP文件来减少页面加载时间.我已经在GZIP文件中转换了所有的JS文件.
现在我想知道如何设置IIS(我正在使用IIS 7)这可以工作.
第二件事我如何在My Asp.net页面中调用GZIP文件?
提前致谢
我创建了一个简单的HttpModule,它在将响应发送到客户端之前从响应中删除空格.这适用于IIS7.0上的aspx页面,但如果我创建一个静态html页面并调用它,HttpModule不会启动(我知道的方式是因为源包含空格,否则本应删除).显然有些东西我做得不对,但不知道是什么.
我的网站位于.NET 4.0和.NET 4.0的应用程序池中ManagedPipelineMode = Integrated.
我已将模块添加为ManagedModule,并引用GAC的强名称密钥程序集.
谢谢
编辑 - 这是web.config中的system.webserver部分
<system.webServer>
...
<modules runAllManagedModulesForAllRequests="true">
<add name="RemoveWhitespaceHttpModule"
type="HttpModules.Modules.RemoveWhitespaceHttpModule, HttpModules,
Version=1.0.0.0, Culture=neutral, PublicKeyToken=8a83u4bi47o9fo0d"
preCondition="" />
</modules>
<defaultDocument>
<files>
<add value="TestForm.aspx" />
</files>
</defaultDocument>
</system.webServer>
Run Code Online (Sandbox Code Playgroud)
编辑 - 修正了它.对于任何感兴趣的人,这是我的模块检查响应然后决定是否继续删除空格的方式
if (contentType.Equals("text/html")
&& httpContext.Response.StatusCode == 200
&& httpContext.CurrentHandler != null)
{ ... }
Run Code Online (Sandbox Code Playgroud)
问题出在httpContext.CurrentHandler!= null之上的第三个条件.当为静态.html页面调用此模块时,currentHandler为null,因此代码永远不会进入操作html.我已经删除了这第三个条件,它现在有效.谢谢大家的回答
asp.net ×9
gzip ×7
iis-7 ×5
c# ×4
iis ×3
web-config ×2
.net ×1
appcmd ×1
asp.net-mvc ×1
c#-4.0 ×1
html ×1
iis-10 ×1
iis-7.5 ×1
jquery ×1
minify ×1
static-html ×1
whitespace ×1