我启动的中间件配置看起来是这样的:ASP.NET 1.0的核心定制压缩中间件和静态文件的问题
public void Configure(IApplicationBuilder app)
{
app.UseCompression();
app.UseIISPlatformHandler();
app.UseApplicationInsightsRequestTelemetry();
app.UseCors("CorsPolicy");
app.UseStaticFiles();
app.UseMvc();
app.UseApplicationInsightsExceptionTelemetry();
}
由于加入app.UseCompression()
中间件,在wwwroot
静态的html文件没有正确加载了。 他们不解决,并往往无限期地加载。
压缩中间件如下所示,并从here是来源:
public class CompressionMiddleware
{
private readonly RequestDelegate nextDelegate;
public CompressionMiddleware(RequestDelegate next)
{
nextDelegate = next;
}
public async Task Invoke(HttpContext httpContext)
{
var acceptEncoding = httpContext.Request.Headers["Accept-Encoding"];
//Checking that we have been given the correct header before moving forward
if (!(String.IsNullOrEmpty(acceptEncoding)))
{
//Checking that it is gzip
if (acceptEncoding.ToString().IndexOf("gzip", StringComparison.CurrentCultureIgnoreCase) >= 0)
{
using (var memoryStream = new MemoryStream())
{
var stream = httpContext.Response.Body;
httpContext.Response.Body = memoryStream;
await nextDelegate(httpContext);
using (var compressedStream = new GZipStream(stream, CompressionLevel.Optimal))
{
httpContext.Response.Headers.Add("Content-Encoding", new string[] { "gzip" });
memoryStream.Seek(0, SeekOrigin.Begin);
await memoryStream.CopyToAsync(compressedStream);
}
}
}
else
{
await nextDelegate(httpContext);
}
}
//If we have are given to Accept Encoding header or it is blank
else
{
await nextDelegate(httpContext);
}
}
}
有谁知道这可能是为什么发生?
注意:我正在使用DNX 1.0.0-rc1-update1
和1.0.0-rc1-final
库。
您是否尝试将'app.UseCompression();'作为最后的指令?注册('.AddXXX'调用)和使用('UseXXX'调用)顺序很重要,因为它确定调用中间件的顺序以及后面的中间件是否覆盖前一个中间件的错误(或要求它工作) – Tseng