【问题标题】:Response Content-length mismatch when running Vue SPA in .Net Core App在 .Net Core App 中运行 Vue SPA 时响应内容长度不匹配
【发布时间】:2019-08-17 06:16:28
【问题描述】:

我正在 .net 核心应用程序中启动 Vue SPA。静态 MVC 页面提供正确,但是当我导航到 SPA 路由时,我收到此错误:

System.InvalidOperationException:响应内容长度不匹配:写入的字节太多(1372 中的 2598)。 在 Microsoft.AspNetCore.Server.Kestrel.Core.Internal.Http.HttpProtocol.VerifyAndUpdateWrite(Int32 计数) 在 Microsoft.AspNetCore.Server.Kestrel.Core.Internal.Http.HttpProtocol.WriteAsync(ReadOnlyMemory`1 数据,CancellationToken cancelToken)

我尝试重新安装所有 node_modules 并使用 UseVueCli 选项,但都没有成功。 Vue 应用在启动 .net 应用之前正在运行。

public class Startup
{
    public Startup(IHostingEnvironment env)
    {
        var builder = new ConfigurationBuilder()
            .SetBasePath(env.ContentRootPath)
            .AddJsonFile("appsettings.json", optional: false, reloadOnChange: true)
            .AddJsonFile($"appsettings.{env.EnvironmentName}.json", optional: false)
            .AddEnvironmentVariables();
        Configuration = builder.Build();
    }

    public IConfigurationRoot Configuration { get; set; }

    public void ConfigureServices(IServiceCollection services)
    {
        services.AddCors(options =>
        {
            options.AddPolicy(
                "*",
                builder =>
                {
                    builder
                        .WithOrigins("")
                        .WithMethods("GET")
                        .AllowAnyHeader()
                        .AllowCredentials();
                });
        });

        services.AddSingleton(Configuration);

        //Add all initial dependencies which can be used anywhere
        services.AddInitialDependencies();

        services.AddCaching();
        services.AddResponseCaching();

        services.AddFrameworks();
        services.AddSecurity(Configuration);
        services.AddDbContexts();
        services.AddSettingsProviders();
        services.AddLogicClasses();
        services.AddRealtimeClasses();

        services.AddAutoMapper((IMapperConfigurationExpression mapperConfiguration) =>
        {
            mapperConfiguration.CreateMissingTypeMaps = false;
        });

        services.AddSignalR();
    }

    public void Configure(IApplicationBuilder app, IHostingEnvironment env, IServiceProvider serviceProvider, IMapper autoMapper)
    {
        app.StartDbConfiguration(serviceProvider);

        app.UseHttpContextLogging();
        app.ConfigureErrorHandling(env);

        app.UseRequestLocalization();

        app.ConfigureHealthChecks();

        app.UseStaticFiles();
        app.UseSpaStaticFiles();

        JwtSecurityTokenHandler.DefaultInboundClaimTypeMap.Clear();

        app.UseAuthentication();
        app.UseClaimsLogging();

        app.UseResponseCaching();

        app.ConfigureRequestValidation();

        app.UseRewriter();

        app.ConfigureRealtime();

        app.UseForwardedHeaders(new ForwardedHeadersOptions()
        {
            ForwardedHeaders = ForwardedHeaders.XForwardedProto
        });

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

        app.UseSpa(spa =>
        {
            if (env.IsDevelopment())
            {
                spa.UseProxyToSpaDevelopmentServer("http://localhost:9051");
            }
        });

        // Configure Kendo UI
        app.UseKendo(env);

        autoMapper.ConfigurationProvider.AssertConfigurationIsValid();
    }
}

当我导航到应该提供 SPA 的路线时,我返回上述错误。

【问题讨论】:

    标签: c# .net visual-studio vue.js .net-core


    【解决方案1】:

    找到了解决办法。 stackify 中间件已注册,这就是问题的原因。将其注释掉即可解决问题。

    【讨论】:

      猜你喜欢
      • 2016-12-02
      • 2020-08-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-10-10
      • 1970-01-01
      • 2019-05-25
      相关资源
      最近更新 更多