.netresthttp-headersopenrasta

How to set Content-Type header charset in OpenRasta


When I return my object as JSON via JsonDataContractCodec OpenRasta sets Content-Type header to

application/json

but ignores charset part of content type.

When I use Chrome it sends GET request with folowing header:

Accept-Charset:windows-1251,utf-8;q=0.7,*;q=0.3

and all my utf-8 encoded json objects goes wrong.

I tried to override OperationResult with no luck. OpenRasta overwrites my header with codec's one.


Solution

  • Just found a way - inherit from JsonCoder and apply the MediaTypeAttribute:

    [MediaType("application/json; charset=utf8")]
    public class JsonWithEncoding:JsonDataContractCodec
    {
    
    }
    

    And register handler with:

    ResourceSpace.Has
      .ResourcesOfType<IEnumerable<ProfileResource>>()
      .AtUri("/profiles")
      .HandledBy<ProfileHandler>()
      .TranscodedBy<JsonWithEncoding>();
    

    But this way is very static:(