响应正文

时间:2016-06-28 20:27:57

标签: asp.net-core asp.net-core-mvc asp.net-core-1.0

我将现有的MVC5 json webservice迁移到在完整框架(NET 4.6.1)上运行的AspNet Core RC2。

每个控制器都继承自_BaseController:

public class _BaseController : Controller
{
   public IActionResult Success(object data) { return Json({ IsSuccess = true, Data = data }); }
   public IActionResult Error(string message) { return Json({ IsSuccess = false, Message = message }); }
}

这是一个样本控制器:

public class SampleController : _BaseController
{
   public IActionResult Something() { return Success(new { FirstItem = "First", SecondItem = "Second", ThirdItem = 1 }); }
}

在RC1上这很好用。回复很好。

更新到RC2后,JSON响应现在包含一些奇怪/无效的字符。

来自Fiddler,这是原始(有效)回复:

{"isSuccess":true,"data":{"firstItem":"first","secondItem":"second","thirdItem":1}}

但RC2提供了这样的无效响应(来自Fiddler的完全原始响应,与.NET winforms客户端收到的响应相同):

HTTP/1.1 200 OK
Transfer-Encoding: chunked
Content-Type: application/json; charset=utf-8
Server: Kestrel
X-Powered-By: ASP.NET
Date: Tue, 28 Jun 2016 20:38:55 GMT

3de
{"sucesso":true,"dados":{"codigo":1,"dataUltimaAtualizacao":"2016-06-27T00:00:00","horaUltimaAtualizacao":"17:18:06.8099844","offsetHoraUltimaAtualizacao":-180,"cnpj":"23303829000169","razaoSocial":"NACIONAL SOFT LTDA","nomeFantasia":"NACIONAL SOFT","tipoEstabelecimento":"MATRIZ","inicioAtividade":"1987-05-13T00:00:00","atividadePrimaria":"62.03-1-00 - Desenvolvimento e licenciamento de programas de computador não-customizáveis","atividadesSecundarias":"62.02-3-00 - Desenvolvimento e licenciamento de programas de computador customizáveis","naturezaJuridica":"206-2 - SOCIEDADE EMPRESARIA LIMITADA","logradouro":"R HORTENCIA","numero":"591","complemento":"CONJUNTO 01","bairro":"ESPLANADA","municipio":"BELO HORIZONTE","uf":"MG","cep":"30280250","enderecoEletronico":"","telefone":"","enteFederativoResponsavel":"*****","situacaoCadastral":"ATIVA","dataSituacaoCadastral":"2005-11-03T00:00:00","motivoSituacaoCadastral":"","situacaoEspecial":"********","dataSituacaoEspecial":null}}
0

这让我疯狂。有没有人知道它是否与Kestrel和IIS之间的集成有关?有什么线索吗?

1 个答案:

答案 0 :(得分:1)

是的,正如CodeCaster所评论的那样,这都是关于chuncked的反应。

我也会尝试修复客户端,但this previous question已修复此问题。

因此,如果有人再遇到此问题,只需在Microsoft.AspNetCore.Buffering上添加对package.json的引用,并在Startup.cs上添加此行:

app.UseResponseBuffering();