How to output a json object of Request.CreateResponse method?
the below code output the json string
"{RowCount:15}"
,the string is not a json ojbect,it should use eval() method of javscript to convert to json object ,I want the server side return the json object directly, It should return
{RowCount:15}
that's a json object.
Code
public class PagedDataAttribute : ActionFilterAttribute
{
public override void OnActionExecuted(HttpActionExecutedContext actionExecutedContext)
{
string jsonRowCount = "{RowCount:10}";
actionExecutedContext.Response = actionExecutedContext.Request.CreateResponse(System.Net.HttpStatusCode.OK, jsonRowCount,System.Net.Http.Formatting.JsonMediaTypeFormatter.DefaultMediaType);
}
}
Instead of using a string, use an anonymous object:
public override void OnActionExecuted(HttpActionExecutedContext actionExecutedContext)
{
var rowCount = new { RowCount = 10 };
actionExecutedContext.Response = actionExecutedContext.Request.CreateResponse(
HttpStatusCode.OK,
rowCount,
JsonMediaTypeFormatter.DefaultMediaType
);
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With