You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
|
|
using Common.Models.ModelValidation; using Common.Models.RequestResponse; using log4net; using Newtonsoft.Json; using System.Collections.Generic; using System.Linq; using System.Net; using System.Net.Http; using System.Web.Http.Controllers; using System.Web.Http.Filters;
namespace ThirdPartyAPIs.CustomFilter { public class ValidationActionFilter : ActionFilterAttribute { private readonly ILog _log = LogManager.GetLogger(typeof(ValidationActionFilter));
public override void OnActionExecuting(HttpActionContext actionContext) { var modelState = actionContext.ModelState; if (!modelState.IsValid) { TPResponse jsonResponse = new TPResponse(); jsonResponse.ResponseCode = "104"; jsonResponse.Msg = "Model Not Valid"; List<FieldsValidation> modelList = new List<FieldsValidation>(); foreach (var item in modelState.Keys) { var err = modelState[item].Errors.ToList(); err.Select(x => { var mes = (string.IsNullOrWhiteSpace(x.ErrorMessage) && string.IsNullOrEmpty(x.ErrorMessage) ? (!string.IsNullOrEmpty(x.Exception.Message) && !string.IsNullOrWhiteSpace(x.Exception.Message) ? x.Exception.Message : "") : x.ErrorMessage); modelList.Add(new FieldsValidation() { Name = item.Split('.')[1], Message = mes }); return x; }).ToList(); } jsonResponse.Data = modelList; var a = JsonConvert.SerializeObject(jsonResponse); //var abc = actionContext.Request.CreateResponse(HttpStatusCode.BadRequest, jsonResponse);
//actionContext.Response = actionContext.Request.CreateResponse(HttpStatusCode.BadRequest, jsonResponse);
//actionContext.Response = actionContext.Request
// .CreateErrorResponse(HttpStatusCode.BadRequest, a.ToString());
_log.Info("Fields Are Not Valid Data:" + a); actionContext.Response = actionContext.Request.CreateErrorResponse(HttpStatusCode.InternalServerError, a.ToString()); } } } }
|