I want to return a json object from the app-app action filter. How can I achieve this?
I can return an object from an action, but I need to return some data from the actionfilter under some condition.
Thanks in advance.
Edit: 1 When I changed the code as shown below, the browser still loads without response and ends in a timeout error.
public class ValidationActionFilter : ActionFilterAttribute { public override void OnActionExecuting(HttpActionContext actionContext) { var modelState = actionContext.ModelState; if (!modelState.IsValid) { List<string> arr = new List<string>(); foreach (var key in modelState.Keys) { var state = modelState[key]; if (state.Errors.Any()) { string er = state.Errors.First().ErrorMessage; if (!string.IsNullOrEmpty(er)) { arr.Add(er); } } } var output = new Result() { Status = Status.Error.ToString(), Data = null, Message = arr }; actionContext.Response = actionContext.Request.CreateResponse(HttpStatusCode.BadRequest, output, actionContext.ControllerContext.Configuration.Formatters.JsonFormatter); } } }
c # asp.net-web-api action-filter
Kumaresan Lc Jun 26 '13 at 10:25 2013-06-26 10:25
source share