In my backend api service, I am using fluent validation. If payload request fail validation, the endpoint returns this
How to copy exception from modelstate to MudBlazor error string array? This is my blazor code.
@page "/marketingeventcreate"
@inject HttpClient Http
@inject NavigationManager NavManager
@using System.Text.RegularExpressions
@using System.ComponentModel.DataAnnotations
@using OnlinePlatform.Support.Domain.Entities
@using OnlinePlatform.Support.Domain.Dtos
<EditForm Model="@model" OnValidSubmit="OnValidSubmit">
<DataAnnotationsValidator/>
<MudGrid>
<MudItem xs="12" sm="7">
<MudCard>
<MudCardContent>
<MudTextField Label="Event Name" HelperText=""
@bind-Value="model.Name" For="@(() => model.Name)"/>
<MudTextField Label="Remark" Class="mt-3"
@bind-Value="model.Remark" For="@(() => model.Remark)"/>
<MudTextField Label="Event Date" HelperText="" Class="mt-3" InputType="InputType.Date"
Mask="@(new DateMask("dd.MM.yyyy"))" DateFormat="dd.MM.yyyy"
@bind-Value="model.EventDate" For="@(() => model.EventDate)" />
<MudTextField Label="Venue" HelperText="" Class="mt-3"
@bind-Value="model.Venue" For="@(() => model.Venue)" />
<MudTextField Label="State" HelperText="" Class="mt-3"
@bind-Value="model.State" For="@(() => model.State)" />
<MudTextField Label="Start Time" HelperText="i.e. 2000" Class="mt-3"
@bind-Value="model.StartTime" For="@(() => model.StartTime)" />
<MudTextField Label="End Time" HelperText="i.e. 2100" Class="mt-3"
@bind-Value="model.EndTime" For="@(() => model.EndTime)" />
</MudCardContent>
<MudCardActions>
<MudButton ButtonType="ButtonType.Submit" Variant="Variant.Filled" Color="Color.Primary" Class="ml-auto">Submit</MudButton>
</MudCardActions>
</MudCard>
</MudItem>
<MudItem xs="12" sm="5">
<MudPaper Class="pa-4 mud-height-full">
<MudText Typo="Typo.subtitle2">Validation Summary</MudText>
@if (success)
{
<MudText Color="Color.Success">Success</MudText>
}
else
{
<MudText Color="@Color.Error">
<ValidationSummary />
</MudText>
}
</MudPaper>
</MudItem>
</MudGrid>
</EditForm>
@code {
MarketingEvent model = new MarketingEvent();
bool success;
string[] errors = { };
private async Task OnValidSubmit(EditContext context)
{
var mydate = new DateTime(model.EventDate.Year, model.EventDate.Month, model.EventDate.Day);
var request = new CreateMarketingEventRequest(model.Name, model.Remark, mydate, model.Venue,
model.State, model.StartTime?.ToString(), model.EndTime?.ToString());
try
{
var response = await Http.PostAsJsonAsync("api/marketingevent", request);
var serverCustomErrors = await response.Content
.ReadFromJsonAsync<Dictionary<string, List<string>>>();
//for (int x = 0; x < serverCustomErrors.Count; x++)
//{
// errors[x] = serverCustomErrors.ElementAt(x).Value.ToArray();
//}
response.EnsureSuccessStatusCode();
if (response.StatusCode == System.Net.HttpStatusCode.Created)
{
success = true;
NavManager.NavigateTo("/");
StateHasChanged();
}
}
catch (Exception ex)
{
}
}
}
In my Swagger, I tested it and the response is in application/json and not application/problem+json. Example,

