I have ApiConroller with method looking like that:
[HttpGet]
public IEnumerable<MyValue> Values()
{
return db.MyValues.ToList();
}
It returns a JSON array. I use jQuery to get results. How do I keep that array from being hijacked, smth like autonesting, etc.?
You can do it in a generic way.
Add the following class:
And now, in your WebApiConfig replace the default
JSonMediaTypeFormatterwith this new one:Now you can return any IEnumerable you wish, like you originally did, i.e.
And the
SecureJsonMEdiaTypeFormatterwill intercept it, and wrap in an anonymous object, underresultproperty: