Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

NancyFx: set default charset to utf8

Tags:

c#

nancy

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

like image 553
Amir Abiri Avatar asked May 11 '14 07:05

Amir Abiri


1 Answers

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";
like image 67
Kamyar Avatar answered Oct 30 '22 10:10

Kamyar