|
- using Microsoft.AspNetCore.Mvc;
- using System;
- using System.Linq;
- using System.Text;
- using HealthMonitor.Util.Entities.Base;
-
- namespace HealthMonitor.WebApi.Controllers.Base
- {
- public class DefaultControllerBase<T> : ControllerBase where T: EntityBase
- {
-
-
-
-
- protected void AssertModelStateIsValid(T? model = null)
- {
- if (!ModelState.IsValid)
- {
- var message = new StringBuilder();
-
- var entityType = typeof(T);
- if (entityType.IsSubclassOf(typeof(HealthMonitorEntityBase))) message.Append("HealthMonitor");
-
-
- message.Append($" [{entityType.FullName}], Error Message: ");
-
- foreach (var kv in ModelState)
- {
- if (kv.Value.ValidationState == Microsoft.AspNetCore.Mvc.ModelBinding.ModelValidationState.Invalid)
- {
- message.Append(string.Join(", ", kv.Value.Errors.Select(e => e.ErrorMessage)));
- }
- }
-
- if (model != null) model.AssertValidate();
- else throw new ArgumentException(message.ToString());
- }
- }
- }
- }
|