laatzen/LaaProductionWeb/LaaProduction.Web/API/APIExtensions.cs

41 lines
1.1 KiB
C#

namespace LaaProduction.Web.API
{
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.Http;
using System.Web.Http.ModelBinding;
using System.Web.Http.Results;
internal static class APIExtensions
{
public static string Content(this ModelStateDictionary modelStateDictionary)
{
var errors = new Dictionary<string, string>();
foreach (var error in modelStateDictionary)
{
var errorKey = error
.Key
?.Split(new[] { '.' }, StringSplitOptions.RemoveEmptyEntries)
?.LastOrDefault();
if (!string.IsNullOrWhiteSpace(errorKey))
{
var errorMessage = error
.Value
.Errors
.FirstOrDefault()
.ErrorMessage;
errors[errorKey] = errorMessage;
}
}
return JsonConvert.SerializeObject(errors, Formatting.Indented);
}
}
}