I've created a middleware delegate in a brand new blazor-server project, to transform my html responses. See code below.
app.Use(async (context, next) =>
{
var originalStream = context.Response.Body;// currently holds the original stream
var newStream = new MemoryStream();
context.Response.Body = newStream;
await next();
string contentType1 = context.Response.ContentType?.Split(';', StringSplitOptions.RemoveEmptyEntries)[0];
string contentType2 = "text/html";
if (contentType1 == "text/html") // EXCEPTION THROWN, SEE IMAGE 1
//if (contentType2 == "text/html") // EXCEPTION NOT THROWN, SEE IMAGE 2
{
newStream.Seek(0, SeekOrigin.Begin);
var originalContent = new StreamReader(newStream).ReadToEnd();
var updatedContent = originalContent.Replace("Hello", "HOLA");
context.Response.Body = originalStream;
await context.Response.WriteAsync(updatedContent);
}
});
But when I compare a string extracted from the Response.ContentType, I'm getting an exception (image 1). But when comparing another string with the same value, I'm not getting it (image 2) In both cases the page is rendered with the replacement. Any help on this one?

