ASP.NET Core响应压缩无法正常工作

时间:2017-12-29 12:36:39

标签: asp.net-core asp.net-core-2.0

我有一个端点从文件加载json然后返回其内容(ASP.NET Core 2.0):

[Produces("application/json")]
[Route("api/[controller]")]
public class ReportsController : Controller
{
    private readonly IHostingEnvironment _hostingEnvironment;

    public ReportsController(IHostingEnvironment hostingEnvironment)
    {
        _hostingEnvironment = hostingEnvironment;
    }

    [HttpGet]
    public IActionResult Get()
    {
        try
        {
            string contentRootPath = System.IO.Path.Combine(_hostingEnvironment.ContentRootPath, "reports");

            return Ok(System.IO.File.ReadAllText(contentRootPath + "\\teste.json"));
        }
        catch (Exception ex)
        {
            return StatusCode(500, new ResponseError(ex.Message));
        }
    }
}

在Chrome中点击此终结点时,我会收到以下网络数据:

enter image description here

现在,如果启用响应压缩,请求的大小会变大:

enter image description here

这是我的ConfigureServicesConfigure方法:

   public void ConfigureServices(IServiceCollection services)
   {
        services.AddResponseCompression(options =>
        {
            options.Providers.Add<GzipCompressionProvider>();
        });

        services.AddApplicationServices();

        services.AddDbContext<DatabaseContext>(...);

        services.AddCors();

        services.AddIdentity<ApplicationUser, IdentityRole>().AddEntityFrameworkStores<DatabaseContext>();

        services.AddAuthentication(JwtBearerDefaults.AuthenticationScheme)
            .AddJwtBearer(options =>
            {
                ...
            });

        services.AddMvc().AddJsonOptions(options => { ... });

        services.AddAuthorization(options =>
        {
            ...
        });

        services.AddEntityFrameworkSqlServer();
    }

    public void Configure(IApplicationBuilder app, IHostingEnvironment env)
    {
        if (env.IsDevelopment())
        {
            app.UseDeveloperExceptionPage();
        }

        app.UseResponseCompression();

        app.UseCors(c => c.AllowAnyOrigin().AllowAnyHeader().AllowAnyMethod());

        app.UseAuthentication();

        app.UseMvc(routes =>
        {
            routes.MapRoute("default", "{controller=Home}/{action=Index}/{id?}");
        });

        app.UseStaticFiles();

    }

我在这里缺少什么?

0 个答案:

没有答案