Base64 encoded JsonResult

I am using Json (object) to return JsonResult in ASP.Net MVC.

One of the properties of the object is a string that must be encoded in Base64.

public class MyClass
{
   public string BlockOfText = "Hello World";
}

Must be converted to the following Json result

{
     "BlockOfText" : "SGVsbG8gV29ybGQ="
}

How can I tell Json Serializer about encoding a Base64 property?

+3
source share
1 answer

One solution is to encode your string server. Before serializing the object, simply do:

var instance.BlockOfText64Base = System.Convert.ToBase64String(Encoding.Default.GetBytes(instance.BlockOfText));
return Json(instance, JsonRequestBehavior.AllowGet);

Hope this helps.

+7
source

All Articles