I have a custom ValidateModelAttribute
but it's not firing for some reason
using System.Net;
using System.Web.Http.Controllers;
using System.Net.Http;
using System.Web.Http.Filters;
namespace blank.Utilities.CustomAttributes
{
public class ValidateModelAttribute : ActionFilterAttribute
{
public override void OnActionExecuting(HttpActionContext actionContext)
{
if (actionContext.ModelState.IsValid == false)
{
actionContext.Response = actionContext.Request.CreateErrorResponse(
HttpStatusCode.BadRequest, actionContext.ModelState);
}
}
}
}
Here is the Post call
[ValidateModelAttribute]
[HttpPost]
[Route("update")]
public IHttpActionResult Post(ActivityPctCompleteDto actDto) {
_activityService.UpdatePctComplete(actDto);
return Ok();
}
I have added fromBody
and fromUri
in the post but it doesn't work. What am I missing here?
Here is my Dto
public class ActivityPctCompleteDto
{
[Required(ErrorMessage = "ActivityID is an invalid Guid value.")]
public Guid? Id { get; set; }
[Required(ErrorMessage = "PctComplete is an invalid Decimal(5,4) value.")]
public decimal? PctComplete { get; set; }
}
I've tried your code and action filter works fine for me. To diagnose your problem check the following:
ActionFilterAttribute
fromSystem.Web.Http.Filters
namespace? There is a common confustion with filters fromSystem.Web.Mvc
.Post()
method processes the request? Try setting breakpoint and check whether it hits.