0

In my ASP.NET Core 2.1 application I tried to set a response header value that contains a non-ASCII character and got the following exception:

System.InvalidOperationException: Invalid non-ASCII or control character in header: 0x00E5 at Microsoft.AspNetCore.Server.Kestrel.Core.Internal.Http.HttpHeaders.ThrowInvalidHeaderCharacter(Char ch) at Microsoft.AspNetCore.Server.Kestrel.Core.Internal.Http.HttpHeaders.ValidateHeaderCharacters(String headerCharacters) at Microsoft.AspNetCore.Server.Kestrel.Core.Internal.Http.HttpHeaders.ValidateHeaderCharacters(StringValues& headerValues) at Microsoft.AspNetCore.Server.Kestrel.Core.Internal.Http.HttpResponseHeaders.SetValueFast(String key, StringValues& value) at Microsoft.AspNetCore.Server.Kestrel.Core.Internal.Http.HttpHeaders.Microsoft.AspNetCore.Http.IHeaderDictionary.set_Item(String key, StringValues value)

OK, fair enough, I'm supposed to encode it - but how? If I call WebUtility.UrlEncode on it that also encodes spaces and slashes, for example, which is obviously not what I want. What framework method am I supposed to call to do the encoding? (Or how do I just get ASP.NET Core to do this for me?)

Edit: It's not a duplicate of that question - I know that I cannot use non-ASCII characters, as it throws an exception! This question is about how to fix that, in ASP.NET Core.

Edit for context: I'm converting an existing application, which uses HttpListener in development and AWS Lambda via API Gateway in production. Both of those return non-ASCII characters just fine. I don't actually want to encode them at all, if I can avoid it, but there doesn't seem to be a way to get Kestrel to allow them.

EM0
  • 5,369
  • 7
  • 51
  • 85
  • You should check how *HTTP* handles headers. This isn't specific to ASP.NET Core. Are you sure the *client* can handle Unicode or any other encoding? – Panagiotis Kanavos Dec 19 '18 at 17:20
  • 1
    Possible duplicate of [Can I use Unicode characters in HTTP headers?](https://stackoverflow.com/questions/7567154/can-i-use-unicode-characters-in-http-headers) – Panagiotis Kanavos Dec 19 '18 at 17:21
  • It is if I don't want to write my own method to do it, which would be silly - surely there must be something built in for this? – EM0 Dec 19 '18 at 17:21
  • I'll repeat the question. Are you sure the *client* can handle whatever encoding is used? Check the linked question - yes, you can. No, there's no agreement on what is allowed. That means you can probably use Url encoding or HTML encoding but whether the *client* understands the header is unknown – Panagiotis Kanavos Dec 19 '18 at 17:23
  • read the question **again**. The problem isn't whether you *can*, it's whether you *should*. Which header are you trying to set? – Panagiotis Kanavos Dec 19 '18 at 17:23
  • It never gets to the client. ASP.NET Core, on the server, *throws an exception*. – EM0 Dec 19 '18 at 17:23
  • 1
    please read the question and the comments. Don't try to reject them out of hand. Yes, you can use **any encoding you like** UrlEncode, HtmlEncode, Base64 take your pick. *Does your client* understand them though? That's what you need to take into account before deciding which one to use – Panagiotis Kanavos Dec 19 '18 at 17:25
  • To put it another way, only *you* can answer this question. What encoding do *you* want to use? The only limitation is that it has to use only ASCII characters – Panagiotis Kanavos Dec 19 '18 at 17:26
  • OK, I think what you're saying is that there is no standard solution. I actually don't want to encode them at all. Yes, the client can handle them. I just want to return any value I want. This works fine if I use, HttpListener, for example - the header is returned with the non-ASCII character. I want the same with Kestrel. – EM0 Dec 19 '18 at 17:29
  • The accepted answer of the https://stackoverflow.com/questions/7567154/can-i-use-unicode-characters-in-http-headers actually also answers this. – ilkerkaran Dec 19 '18 at 17:31
  • "Don't do what you want to do" is not a useful answer, even if it is the accepted answer for another question. – EM0 Dec 19 '18 at 17:33
  • "I'd like to travel to the moon, but I want to go by train". At some point, someone's gonna tell you not to do that. – Roger Lipscombe Dec 19 '18 at 17:34
  • _Why_ do you want non-ASCII characters in your HTTP headers? – Roger Lipscombe Dec 19 '18 at 17:35
  • Because the existing application, using HttpListener did that, and the client and the server work perfectly well with that. It also works in AWS Lambda using API Gateway, by the way. So this is hardly analogous to "going to the moon by train". In any case, whether it's reasonable to do what I want or not, if ASP.NET Core doesn't allow it, then there is nothing more to be said. – EM0 Dec 19 '18 at 17:39

0 Answers0