Question

I have this snippet in my cshtml file:

Expires on: @Model.EndDate.ToString("MMM dd yyyy")

And I get this in the response:

HTTP/1.1 200 OK
Content-Type: text/html

...
Expires on: ׳�׳�׳™ 05 2013

How do I tell Nancy to use UTF8 by default for responses?


Edit: To clarify, this isn't a localization problem, the output is already localized - it's just that the localized UTF8 string is sent to the client without a UTF8 charset declaration, so it gets mucked up in an attempt to treat it as latin1.

What I am looking for is this:

HTTP/1.1 200 OK
Content-Type: text/html; charset=utf-8

...

And I'd like to not have to specify it for each response individually.

I am using the NancyFx web framework

Was it helpful?

Solution

If you want to specify UTF8 charset declaration in your module's response, you can define an After interceptor. Here's how you can define an After Interceptor in your module's constructor:

After += ctx =>
{
    ...
}

You can also define Application-level hook in your bootstrapper:

pipelines.AfterRequest += (ctx) => { ... };

Update: Based on the comments, best approach would be using the following code in the hook:

if (ctx.Response.ContentType == "text/html")
    ctx.Response.ContentType = "text/html; charset=utf-8";
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top