Ejemplo n.º 1
0
        /// <summary>
        /// Creates a JSON response if an error occurred, appending all <see cref="Controller.ModelState"/> errors
        /// to the response object
        /// </summary>
        /// <param name="errorMessage">The error message to display to the user, this value cannot be null</param>
        /// <see cref="CreateJsonResponse"/>
        public static object CreateJsonErrorResponse(this Controller controller, [NotNull]string errorMessage)
        {
            var data = controller
                .ModelState
                .Where(kvp => kvp.Value.Errors.Any())
                .ToDictionary(kvp => kvp.Key, kvp => kvp.Value.Errors.Implode(error => error.ErrorMessage, ", "));

            return controller.CreateJsonResponse(errorMessage, data);
        }
Ejemplo n.º 2
0
 /// <summary>
 /// Serializes <paramref name="data"/> to a <c>JSON</c> string and returns it as
 /// <c>application/json</c>
 /// </summary>
 /// <param name="errorMessage">The error message to display to the user or <c>null</c> if no error occurred</param>
 /// <param name="data">The data to serialize to a JSON string</param>
 public static ActionResult GetJsonResult(this Controller controller, string errorMessage = null, object data = null)
 {
     return new InjectableJsonResult { Data = controller.CreateJsonResponse(errorMessage, data) };
 }
Ejemplo n.º 3
0
 /// <summary>
 /// Creates a JSON response from an <c>Exception</c>
 /// </summary>
 /// <see cref="CreateJsonResponse"/>
 /// <seealso cref="CreateJsonErrorResponse(System.Web.Mvc.Controller,string)"/>
 public static object CreateJsonErrorResponse(this Controller controller, [NotNull]Exception exception)
 {
     return controller.CreateJsonResponse(exception.Message);
 }