The data method in the sample data controller should not return an IEnumerable directly. #622

Closed
opened 2025-08-09 17:17:00 +00:00 by fergalmoran · 0 comments
Owner

Originally created by @bbarry on 6/29/2017

Looking at the controller for data for the angular sample (but the issue appears to apply to all of them) I see:

[HttpGet("[action]")]
public IEnumerable<WeatherForecast> WeatherForecasts()
...

Returning data this way from an action results in a json object representing an array and enables consumers to depend on the array type as a top level artifact. If in the future the result would need to be enhanced (for example to something representing paged data), consumers could be broken by the change from an array to an object.

It would be better instead to promote usage of a simple data structure containing the enumerable on a well known path:

[HttpGet("[action]")]
public Result<IEnumerable<WeatherForecast>> WeatherForecasts()
...

public class Result<T>
{
    T Data { get; set; }
}
*Originally created by @bbarry on 6/29/2017* Looking at the controller for data for the angular sample (but the issue appears to apply to all of them) I see: [HttpGet("[action]")] public IEnumerable<WeatherForecast> WeatherForecasts() ... Returning data this way from an action results in a json object representing an array and enables consumers to depend on the array type as a top level artifact. If in the future the result would need to be enhanced (for example to something representing paged data), consumers could be broken by the change from an array to an object. It would be better instead to promote usage of a simple data structure containing the enumerable on a well known path: [HttpGet("[action]")] public Result<IEnumerable<WeatherForecast>> WeatherForecasts() ... public class Result<T> { T Data { get; set; } }
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: github/JavaScriptServices#622
No description provided.