【问题标题】:On Azure I have JSON responses with extra spaces after \n在 Azure 上,我在 \n 之后有带有额外空格的 JSON 响应
【发布时间】:2020-01-10 11:33:12
【问题描述】:

我使用 ASP.NET Core 2.2 创建了一些 web-api,它们返回 JSON 响应。

但是,在我的本地调试中,当我拨打电话时,一切似乎都正常,但是当我在我的 JSON 响应中继续在 Azure 上发布时,我在播种过程中插入的每个 \n 之后都会看到额外的空格。

所以对于同一个请求,我有这个本地响应:

{"id":57,"content":"First Line. \nSecond Line.\nThird Line","otherAttrubute":"otherValue"}

还有这个实时(或远程,如果你喜欢)版本:

{"id":57,"content":"First Line. \r\n    Second Line.\r\n    Third Line","otherAttrubute":"otherValue"}

我在DbContextOnModelCreating 中播种我的数据库:

protected override void OnModelCreating(ModelBuilder modelBuilder)
{
    var myObjects = new MyObject[]
    {
        [previous objects],
        new MyObject { Id=57, Content "First Line. \nSecond Line.\nThird Line", OtherAttrubute="otherValue" },
        [following objects]
    };

    modelBuilder.Entity<MyObject>().HasData(myObjects);
    base.OnModelCreating(modelBuilder);
}

在我的控制器中,我还尝试在Content 属性上使用Trim(),然后用return Ok(myObject) 返回我的对象​​,但它不起作用。

我的猜测是这种行为可能是由 Azure SQL 数据库引起的,但我不确定。

如果您需要查看更多代码,请询问。 谢谢

【问题讨论】:

    标签: entity-framework azure azure-sql-database asp.net-core-webapi


    【解决方案1】:
    \n = CR (Carriage Return) // Used as a new line character in Unix
    \r = LF (Line Feed) // Used as a new line character in Mac OS
    \n\r = CR + LF // Used as a new line character in Windows
    

    此问题通常发生在操作系统不一致时。看来字符串又被转义了。

    你可以通过remote debugging with VS找到根本原因

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2022-01-12
      • 2022-11-01
      • 2020-12-14
      • 2017-01-29
      • 1970-01-01
      • 2017-02-12
      • 1970-01-01
      • 2015-09-27
      相关资源
      最近更新 更多