0

I am trying to resolve two errors on my kestrel middleware which is designed to handle a fetch request and return some JSON. (I am not interested in using MVC)

The code works, but I would like to remove these two errors:

In chrome I get this error:

net::ERR_INCOMPLETE_CHUNKED_ENCODING 200 (OK)

In kestrel I get this error:

"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);
                    }
    })
}

In typescript I fetch like this:

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

2 Answers2

0

With ASP.NET Core, you will "use MVC" (eg: in your startup.cs), but that doesn't mean you have to use MVC (views, etc)... You'll be using MVC namespaces, etc, but you can still return JSON data. There are a couple classes that are aimed at APIs than a traditional MVC view response -

The first would be ControllerBase - https://learn.microsoft.com/en-us/dotnet/api/microsoft.aspnetcore.mvc.controllerbase?view=aspnetcore-2.2

The second class that may be of interest would be the ApiControllerAttribute class

Take a look at this article - https://learn.microsoft.com/en-us/aspnet/core/web-api/?view=aspnetcore-2.2

Tony Ranieri
  • 1,479
  • 1
  • 18
  • 32
0

Well that was dumb, I had an extra

await next();

Which caused the problems

patrick
  • 16,091
  • 29
  • 100
  • 164