Change responce encoding

[AddHeader(ContentType = "text/plain")]
public object Any(Update request)
{
    Encoding.RegisterProvider(CodePagesEncodingProvider.Instance);
    var encoding = Encoding.GetEncoding("windows-1251");

    var str = File.ReadAllText(@"D:\curl_out.txt", encoding);

    return str;
}

despite that encoding windows-1251 the answer would still utf-8, as can be done to the responce was in encoding windows-1251?

Thx.

There is no “source” encoding in C# strings which are always stored as UTF-16 in CLR string or char values and ServiceStack always writes responses in UTF-8.

If you want to return a different encoding you’ll need to write it directly to the response yourself, e.g:

public async Task Get(Update request)
{
    //...
    Response.ContentType = "text/plain; charset=windows-1251";
    var bytes = encoding.GetBytes(str);
    await Response.OutputStream.WriteAsync(bytes, 0, bytes.Length);
    Response.EndRequest(skipHeaders:true);
}
1 Like