I used this below code on my asp.net controller to return Json object on my Ajax on javascript
public JsonResult myMethod()
{
// return a Json Object, yo
If you create yourself a new HttpContent class for delivering JSON, like...
public class JsonContent : HttpContent {
private readonly MemoryStream _Stream = new MemoryStream();
public JsonContent(object value) {
Headers.ContentType = new MediaTypeHeaderValue("application/json");
var jw = new JsonTextWriter( new StreamWriter(_Stream));
jw.Formatting = Formatting.Indented;
var serializer = new JsonSerializer();
serializer.Serialize(jw, value);
jw.Flush();
_Stream.Position = 0;
}
protected override Task SerializeToStreamAsync(Stream stream, TransportContext context) {
return _Stream.CopyToAsync(stream);
}
protected override bool TryComputeLength(out long length) {
length = _Stream.Length;
return true;
}
}
Then you can do,
public HttpResponseMessage Get() {
return new HttpResponseMessage() {
Content = new JsonContent(new
{
Success = true, //error
Message = "Success" //return exception
})
};
}
just like you do with JsonResult.