how to validate request body in c#
[Valid] //here we apply the filter and request made to this model is validated by validation rules on the model
[HttpPost]
public HttpResponseMessage someMethod(SomeValidationModel someValidationModel)
{
//some logic
}
// define your Reusable Model validation filter class
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web;
using System.Web.Http.Controllers;
using System.Web.Http.Filters;
namespace mynamespace.filters
{
public class ValidAttribute : ActionFilterAttribute
{
public override void OnActionExecuting(HttpActionContext actionContext)
{
if (!actionContext.ModelState.IsValid)
{
actionContext.Response = actionContext.Request.CreateErrorResponse(
HttpStatusCode.BadRequest, actionContext.ModelState);
}
}
}
}