Saturday, 29 June 2019

web api: Action filter

using System.Net;
using System.Net.Http;
using System.Web.Http.Controllers;
using System.Web.Http.Filters;

namespace TDX.Filters
{
    /// <summary>
    /// <see cref="ActionFilterAttribute"/> used to specify that the model state should be validated.
    /// </summary>
    public class ValidateModelStateFilter : ActionFilterAttribute
{
#region Public Members

/// <summary>
/// Override used to check the model state for a given request and return an error response if it is invalid.
/// </summary>
/// <param name="actionContext">The <see cref="HttpActionContext"/> associated with the request.</param>
public override void OnActionExecuting(HttpActionContext actionContext)
{
if (!actionContext.ModelState.IsValid)
{
actionContext.Response = actionContext.Request.CreateErrorResponse((HttpStatusCode)422, actionContext.ModelState);
}
}

#endregion
}
}

No comments:

Post a Comment