I need to return a HttpStatusCodeResult object with description, which contains special (diacritics) characters. I don't know how to set proper encoding.
return new HttpStatusCodeResult(HttpStatusCode.BadRequest, "Zażółć gęsią jaźń.");
Should return:
Zażółć gęsią jaźń.
but I get:
Za¿ó³æ gêsi¹ jañ.
I was trying with Response.Charset = System.Text.Encoding.UTF8.WebName;
etc. but still nothing.
You shouldn't get "Za¿ó³æ gêsi¹ jañ." anywhere.
But you can't get "Zażółć gęsią jaźń." in the HTTP status line reason phrase because it only supports the ASCII character set and encoding [RFC 7230 ]. (HTTP header fields do have a mechanism for %-encoding, etc., but not the status line.)
So, you could leave the reason phrase out and put the message in the body:
Response.StatusCode = (Int32)HttpStatusCode.BadRequest;
return Content("Zażółć gęsią jaźń. And, the quick brown fox jumps over the lazy dog. 😉");
Use whatever content you want, including HTML. But, the web browser that receives this might override the content if it's not long enough to seem useful and modern. You should also consider if the web server is configured to replace error pages. This could help:
<configuration>
<system.webServer>
<httpErrors existingResponse="PassThrough" />
</system.webServer>
</configuration>