I am using asp.net mvc 4. Looking at some tutorials I have created my own JsonResult class inheriting from OOB JsonResult class to use Json.Net. Below is how my class looks.
public class JsonNetResult : JsonResult
{
private readonly object _data;
public JsonNetResult(object data)
{
if (data == null)
{
throw new ArgumentNullException("data");
}
_data = data;
}
public override void ExecuteResult(ControllerContext context)
{
if (context == null)
{
throw new ArgumentNullException("context");
}
var response = context.HttpContext.Response;
response.ContentType = "application/json";
var writer = new JsonTextWriter(response.Output);
var serializer = JsonSerializer.Create(new JsonSerializerSettings());
serializer.Serialize(writer, _data);
writer.Flush();
}
}
What I wanted to ask is
- whether this class is required, or .net is using Json.Net internally for serializing the object.
- Also can I directly bind a serialized model to the view.
JavaScriptSerializerclass to achieve it.string, but why would you want to do that?