ich die Fehler vom Typ Required
aus dem Modellzustand, bei PATCH HTTP-Anforderung zu ignorieren, wobei ich Teil-Updates am erlaubt:Fehler vom Typ entfernen von Model Erforderliche
public class ValidateModelFilterAttribute : ActionFilterAttribute
{
public override void OnActionExecuting(ActionExecutingContext context)
{
if (!context.ModelState.IsValid && context.HttpContext.Request.Method == "PATCH")
{
// can't figure out this part
var modelStateErrors = context.ModelState.Keys.SelectMany(key => context.ModelState[key].Errors);
// get errors of type required from the modelstate
// context.ModelState.Remove("attribute_which_failed_due_to_required");
}
if (!context.ModelState.IsValid)
{
var modelErrors = new Dictionary<string, Object>();
modelErrors["message"] = "The request has validation errors.";
modelErrors["errors"] = new SerializableError(context.ModelState);
context.Result = new BadRequestObjectResult(modelErrors);
}
}
}
Controller Aktion:
[ValidateModelFilter]
[HttpPatch("{id}")]
public virtual async Task<IActionResult> Update([FromRoute] int id, [FromBody] TEntity updatedEntity)
{
TEntity entity = repository.GetById<TEntity>(id);
if (entity == null)
{
return NotFound(new { message = $"{EntityName} does not exist!" });
}
repository.Update(entity, updatedEntity);
await repository.SaveAsync();
return NoContent();
}
Also, wie kann ich "erforderliche" Typfehler herausfiltern und sie aus dem Modellzustand entfernen.
Code stürzt parameterParts enthält keinen Punkt –
Okay, geben Sie bitte Ihre Controller-Methode zu Ihrer Frage hinzufügen. Fügen Sie auch ModelState.Keys hinzu, um Dinge zu verdeutlichen. –
@IzzetYildrim ModelState.Keys gibt einfach Schlüsselnamen wie ID, Name, Farbe usw. zurück. Es enthält sogar falsche Schlüsselnamen, wenn falsche Schlüsselnamen im Körper geschrieben werden. –