在 kestrel 中获取请求中间件返回 net::ERR_INCOMPLETE_CHUNKED_ENCODING

Fetch request middleware in kestrel returning net::ERR_INCOMPLETE_CHUNKED_ENCODING

我正在尝试解决我的 kestrel 中间件上的两个错误,该中间件旨在处理获取请求和 return 一些 JSON。 (我对使用 MVC 不感兴趣)

代码有效,但我想删除这两个错误:

在 chrome 我得到这个错误:

net::ERR_INCOMPLETE_CHUNKED_ENCODING 200 (OK)

在 kestrel 中我得到这个错误:

"statuscode cannot be set because the response has already started"

        public void Configure(
            IApplicationBuilder app,
            IHostingEnvironment env,
            ILoggerFactory loggerFactory)
            {
    // some startup stuff...

        app.Use(async (context, next) =>
                {
                    if (context.Request.Path.HasValue && context.Request.Path.Value.Contains("searchjsfetch/"))
                    {
                        await context.Response.WriteAsync(JsonConvert.SerializeObject(simpleObject), Encoding.UTF8);
                    }
    })
}

在打字稿中我是这样获取的:

fetch(`${hostDomain}searchjsfetch/${email}/2/3`)
                    .then((response) => {
                    response.body.getReader().read().then((c) => {
                            return new TextDecoder("utf-8").decode(c.value);
                        });
                    })

使用 ASP.NET 核心,您将 "use MVC"(例如:在您的 startup.cs 中),但这并不意味着您必须使用 MVC(视图等)。 . 您将使用 MVC 名称空间等,但您仍然可以 return JSON 数据。有几个 classes 是针对 API 而不是传统的 MVC 视图响应 -

第一个是 ControllerBase - https://docs.microsoft.com/en-us/dotnet/api/microsoft.aspnetcore.mvc.controllerbase?view=aspnetcore-2.2

可能感兴趣的第二个 class 是 ApiControllerAttribute class

看看这篇文章 - https://docs.microsoft.com/en-us/aspnet/core/web-api/?view=aspnetcore-2.2

好吧,那太蠢了,我有一个额外的

await next();

导致问题的原因