public override void OnActionExecuting(HttpActionContext context) { ModuleRepository moduleRepository = new ModuleRepository(new MyRoomDbContext()); bool hasChildrens = moduleRepository.HasCategoriesChildrens((int)context.ActionArguments["key"]); if (!hasChildrens) throw new HttpResponseException(context.Request.CreateErrorResponse(HttpStatusCode.NotAcceptable, "Please, delete the categories children")); }