【发布时间】:2019-08-09 04:01:51
【问题描述】:
我正在尝试解决我的 kestrel 中间件上的两个错误,该中间件旨在处理获取请求并返回一些 JSON。 (我对使用 MVC 不感兴趣)
代码有效,但我想删除这两个错误:
在 chrome 中我得到这个错误:
net::ERR_INCOMPLETE_CHUNKED_ENCODING 200(正常)
在红隼中我得到这个错误:
“响应已经开始,无法设置状态码”
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);
});
})
【问题讨论】:
-
您希望获取什么?中间件是您可以注入到请求/响应管道中的东西,但通常您不会只使用中间件来处理完整的请求。
-
只是一些 JSON 数据
-
您能否详细说明您对 MVC 的担忧是什么让您想避免它?
标签: asp.net-core kestrel-http-server