Skip to content

Instantly share code, notes, and snippets.

View AndyButland's full-sized avatar

Andy Butland AndyButland

View GitHub Profile
{
"values": {
"name": "Fred",
"favouriteColours": ["red", "green"],
},
}
public class MyApiControllerAttribute : ActionFilterAttribute
{
/// <inheritdoc/>
public override void OnActionExecuted(ActionExecutedContext ctx)
{
if (ctx.Result is ObjectResult objectResult)
{
// Create options to match the CMS delivery API, along with any customizations we want for Forms.
var options = new JsonSerializerOptions
{
public class MyApiControllerAttribute : ActionFilterAttribute
{
public override void OnActionExecuted(ActionExecutedContext ctx)
{
if (ctx.Result is ObjectResult objectResult)
{
var settings = new JsonSerializerSettings
{
NullValueHandling = NullValueHandling.Ignore,
ContractResolver = new MyApiContractResolver(),
public class FormEntryDtoModelBinder : IModelBinder
{
public async Task BindModelAsync(ModelBindingContext bindingContext)
{
if (bindingContext == null)
{
throw new ArgumentNullException(nameof(bindingContext));
}
using (var reader = new StreamReader(bindingContext.HttpContext.Request.Body))
public class FormEntryDtoModelBinder : IModelBinder
{
public async Task BindModelAsync(ModelBindingContext bindingContext)
{
if (bindingContext == null)
{
throw new ArgumentNullException(nameof(bindingContext));
}
using (var reader = new StreamReader(bindingContext.HttpContext.Request.Body))
public async Task<ActionResult> SubmitEntry(
Guid id,
[FromBody] [ModelBinder(typeof(FormEntryDtoModelBinder))] FormEntryDto entry)
{
}
private static void ApplyPaging(PackageSearchQuery query, SearchOptions options)
{
options.Skip = (query.PageNumber - 1) * query.PageSize;
options.Size = query.PageSize;
}
private static void ApplyOrderBy(SearchOptions options, PackageSearchQuery query)
{
switch (query.OrderBy)
{
case PackageSearchOrderBy.Alphabetical:
options.OrderBy.Add(nameof(PackageModel.Title));
break;
case PackageSearchOrderBy.ReverseAlphabetical:
options.OrderBy.Add($"{nameof(PackageModel.Title)} desc");
break;
private static void ApplyFilter(SearchOptions options, PackageSearchQuery query)
{
var filter = new StringBuilder();
bool isFiltered = false;
// If category provided, return packages that match on the category Id.
if (query.CategoryId.HasValue)
{
filter.AppendFormat("{0} eq '{1}'", $"{nameof(PackageModel.Category)}/{nameof(PackageModel.Category.Id)}", query.CategoryId.Value);
isFiltered = true;
private static void SelectFields(SearchOptions options, PackageSearchQuery query)
{
SelectDefaultFields(options);
if (!query.Fields.Any())
{
return;
}
foreach (var field in query.Fields)