|
4 | 4 | [](https://www.nuget.org/packages/PartialResponse.AspNetCore.Mvc.Formatters.Json) |
5 | 5 | [](https://www.myget.org/feed/partialresponse/package/nuget/PartialResponse.AspNetCore.Mvc.Formatters.Json) |
6 | 6 | [](https://ci.appveyor.com/project/dotarj/partialresponse-aspnetcore-mvc-formatters-json) |
7 | | -[](https://codecov.io/gh/dotarj/PartialResponse.AspNetCore.Mvc.Formatters.Json) |
| 7 | +[](https://codecov.io/gh/dotarj/PartialResponse.AspNetCore.Mvc.Formatters.Json) |
| 8 | + |
| 9 | +PartialResponse.AspNetCore.Mvc.Formatters.Json provides JSON partial response support for ASP.NET Core MVC. This package is also [available for ASP.NET Web API](https://www.nuget.org/packages/WebApi.PartialResponse/). |
| 10 | + |
| 11 | +## Getting started |
| 12 | + |
| 13 | +First, add a dependency to PartialResponse.AspNetCore.Mvc.Formatters.Json using the NuGet package manager or by adding a package reference to the .csproj: |
| 14 | + |
| 15 | +```xml |
| 16 | +<ItemGroup> |
| 17 | + <PackageReference Include="PartialResponse.AspNetCore.Mvc.Formatters.Json" Version="x.x.x" /> |
| 18 | +</ItemGroup> |
| 19 | +``` |
| 20 | + |
| 21 | +Then, remove the `JsonOutputFormatter` from the output formatters and add the `PartialJsonOutputFormatter`: |
| 22 | + |
| 23 | +```csharp |
| 24 | +public void ConfigureServices(IServiceCollection services) |
| 25 | +{ |
| 26 | + services |
| 27 | + .AddMvc(options => options.OutputFormatters.RemoveType<JsonOutputFormatter>()) |
| 28 | + .AddPartialJsonFormatters(); |
| 29 | +} |
| 30 | +``` |
| 31 | + |
| 32 | +The `fields` parameter value, which is used to filter the API response, is case-sensitive by default, but this can be changed using the `MvcPartialJsonOptions`: |
| 33 | + |
| 34 | +```csharp |
| 35 | +public void ConfigureServices(IServiceCollection services) |
| 36 | +{ |
| 37 | + services.Configure<MvcPartialJsonOptions>(options => options.IgnoreCase = true); |
| 38 | +} |
| 39 | +``` |
| 40 | + |
| 41 | +That's it! |
| 42 | + |
| 43 | +## Understanding the fields parameter |
| 44 | + |
| 45 | +The `fields` parameter filters the API response so that the response only includes a specific set of fields. The `fields` parameter lets you remove nested properties from an API response and thereby reduce your bandwidth usage. |
| 46 | + |
| 47 | +The following rules explain the supported syntax for the `fields` parameter value, which is loosely based on XPath syntax: |
| 48 | + |
| 49 | +* Use a comma-separated list (`fields=a,b`) to select multiple fields. |
| 50 | +* Use an asterisk (`fields=*`) as a wildcard to identify all fields. |
| 51 | +* Use parentheses (`fields=a(b,c)`) to specify a group of nested properties that will be included in the API response. |
| 52 | +* Use a forward slash (`fields=a/b`) to identify a nested property. |
| 53 | + |
| 54 | +In practice, these rules often allow several different `fields` parameter values to retrieve the same API response. For example, if you want to retrieve the playlist item ID, title, and position for every item in a playlist, you could use any of the following values: |
| 55 | + |
| 56 | +* `fields=items/id,playlistItems/snippet/title,playlistItems/snippet/position` |
| 57 | +* `fields=items(id,snippet/title,snippet/position)` |
| 58 | +* `fields=items(id,snippet(title,position))` |
| 59 | + |
| 60 | +**Note:** As with all query parameter values, the fields parameter value must be URL encoded. For better readability, the examples in this document omit the encoding. |
| 61 | + |
| 62 | +**Note:** Due to the relatively slow performance of LINQ to JSON (Json.NET), the usage of PartialJsonOutputFormatter has a performance impact compared to the regular Json.NET serializer. Because of the reduced traffic, the overhead in time could be neglected. |
0 commit comments