Asp.net core ASP.NET Core 2.1中带有React(CRA)的响应压缩
以下是进行响应压缩的官方文件: 我跟踪了文档,发现只有在调试时它才起作用(index.html、bundle.js被压缩)。发布后,该应用程序没有压缩任何内容。我应该怎么做才能使它在生产中发挥作用Asp.net core ASP.NET Core 2.1中带有React(CRA)的响应压缩,asp.net-core,asp.net-core-2.1,Asp.net Core,Asp.net Core 2.1,以下是进行响应压缩的官方文件: 我跟踪了文档,发现只有在调试时它才起作用(index.html、bundle.js被压缩)。发布后,该应用程序没有压缩任何内容。我应该怎么做才能使它在生产中发挥作用 // Program.cs public class Program { public static IWebHostBuilder CreateWebHostBuilder(string[] args) { return WebHost.CreateDefaultB
// Program.cs
public class Program
{
public static IWebHostBuilder CreateWebHostBuilder(string[] args)
{
return WebHost.CreateDefaultBuilder(args)
.ConfigureAppConfiguration((hostingContext, config) =>
{
config.AddAppSettingsFromDockerSecrets();
})
.ConfigureServices(services =>
{
services.AddResponseCompression();
services.AddResponseCaching();
})
.Configure(app =>
{
app.UseResponseCompression();
app.UseResponseCaching();
app.Use(async (context, next) =>
{
context.Response.GetTypedHeaders().CacheControl = new CacheControlHeaderValue()
{
Public = true,
MaxAge = TimeSpan.FromSeconds(5),
};
context.Response.Headers[HeaderNames.Vary] = new string[] { "Accept-Encoding" };
await next();
});
})
.UseStartup<Startup>();
}
public static void Main(string[] args)
{
CreateWebHostBuilder(args).Build().Run();
}
}
// Startup.cs
public class Startup
{
public Startup(IConfiguration configuration, ILogger<Startup> logger)
{
this.Configuration = configuration;
this.Logger = logger;
}
public IConfiguration Configuration { get; }
public ILogger Logger { get; }
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
public void Configure(IApplicationBuilder app, IHostingEnvironment env)
{
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}
else
{
app.UseHsts();
}
app.UseHttpsRedirection();
app.UseStaticFiles();
app.UseSpaStaticFiles();
app.UseMvc(routes =>
{
routes.MapRoute(
name: "default",
template: "{controller}/{action=Index}/{id?}");
});
app.UseSpa(spa =>
{
spa.Options.SourcePath = "ClientApp";
if (env.IsDevelopment())
{
spa.UseReactDevelopmentServer(npmScript: "start");
}
});
}
// This method gets called by the runtime. Use this method to add services to the container.
public void ConfigureServices(IServiceCollection services)
{
services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_1);
// In production, the React files will be served from this directory
services.AddSpaStaticFiles(configuration =>
{
configuration.RootPath = "ClientApp/build";
});
}
}
//Program.cs
公共课程
{
公共静态IWebHostBuilder CreateWebHostBuilder(字符串[]args)
{
返回WebHost.CreateDefaultBuilder(args)
.ConfigureAppConfiguration((hostingContext,config)=>
{
config.addAppSettingsFromDockerCrets();
})
.ConfigureServices(服务=>
{
services.AddResponseCompression();
services.addResponseCache();
})
.配置(应用=>
{
app.UseResponseCompression();
app.UseResponseCaching();
应用程序使用(异步(上下文,下一步)=>
{
context.Response.GetTypedHeaders().CacheControl=new CacheControlHeaderValue()
{
公共=真实,
最大年龄=从秒(5)开始的时间跨度,
};
context.Response.Headers[HeaderNames.Vary]=新字符串[]{“接受编码”};
等待下一个();
});
})
.UseStartup();
}
公共静态void Main(字符串[]args)
{
CreateWebHostBuilder(args.Build().Run();
}
}
//Startup.cs
公营创业
{
公共启动(IConfiguration配置,ILogger记录器)
{
配置=配置;
this.Logger=Logger;
}
公共IConfiguration配置{get;}
公共ILogger记录器{get;}
//此方法由运行时调用。请使用此方法配置HTTP请求管道。
公共无效配置(IApplicationBuilder应用程序,IHostingEnvironment环境)
{
if(env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}
其他的
{
app.UseHsts();
}
app.UseHttpsRedirection();
app.UseStaticFiles();
app.UseSpaStaticFiles();
app.UseMvc(路由=>
{
routes.MapRoute(
名称:“默认”,
模板:“{controller}/{action=Index}/{id?}”);
});
app.UseSpa(spa=>
{
spa.Options.SourcePath=“ClientApp”;
if(env.IsDevelopment())
{
用户eactdevelopmentserver(npmScript:“开始”);
}
});
}
//此方法由运行时调用。请使用此方法将服务添加到容器中。
public void配置服务(IServiceCollection服务)
{
services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_1);
//在生产中,React文件将从此目录提供服务
services.AddSpaStaticFiles(配置=>
{
configuration.RootPath=“ClientApp/build”;
});
}
}
您能提供更多关于该案例的详细信息吗?你能发布你的Startup.cs和WebHost创建吗?你是如何发布你的应用程序的,它是一个简单的“网络发布”吗?另外,您如何验证您的资源没有被压缩?文档中描述的设置很好,因此我们需要更多的细节来帮助您。我检查了响应头的内容编码。调试时,它显示了gzip。发布后,它是空的。您在生产中使用SSL吗?如果是这样的话,您需要为HTTPS设置EnableForHttps
。请尝试一下。没有区别。