Skip to content

Instantly share code, notes, and snippets.

Created March 29, 2013 05:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save anonymous/5268878 to your computer and use it in GitHub Desktop.
Save anonymous/5268878 to your computer and use it in GitHub Desktop.
public class NewtonSoftJsonResult : JsonResult
{
public override void ExecuteResult(ControllerContext context)
{
if (context == null)
throw new ArgumentNullException("context");
var response = context.HttpContext.Response;
response.ContentType = ContentType;
response.ContentEncoding = ContentEncoding;
if (Data == null)
return;
var serializer = new JsonSerializerSettings
{
ReferenceLoopHandling = ReferenceLoopHandling.Ignore,
ContractResolver = new NHibernateContractResolver()
};
string serializedObject = JsonConvert.SerializeObject(Data, serializer);
response.Write(serializedObject);
}
}
public class NHibernateContractResolver : DefaultContractResolver
{
protected override JsonContract CreateContract(Type objectType)
{
if (typeof(NHibernate.Proxy.INHibernateProxy).IsAssignableFrom(objectType))
return base.CreateContract(objectType.BaseType);
else
return base.CreateContract(objectType);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment