Domanda

I try to build a parametered middleware, but I only get http 504 error. This code works fine without the parameter

any idea?

public void Configuration(IAppBuilder app)
{
    app.Use<MyMiddleware>("Hello");
}

class MyMiddleware : OwinMiddleware
{
    public MyMiddleware(OwinMiddleware next, string message)
    : base(next)
    {
        Message = message;
    }

    public string Message  { get; set; }

    public override async Task Invoke(IOwinContext context)
    {
        context.Response.ContentLength = Message.Length;
        context.Response.ContentType = "text/html";
        await context.Response.WriteAsync(Message);
        await Next.Invoke(context);
    }
}
È stato utile?

Soluzione

Ok I found the issue

ContentLength should be evaluated in Utf-8 like this:

Encoding.UTF8.GetBytes(Message).Length

c# uses utf-16 encoding by default

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top