Esempio n. 1
0
            /// <summary>
            /// Serializes to json.
            /// </summary>
            /// <param name="serialization">The serialization.</param>
            /// <returns></returns>
            public override string Serialize(Dictionary <string, object> serialization)
            {
                Dictionary <string, object> response = SerializedResult.BuildResponse(_serializableObject, serialization);

                Dictionary <string, object> wrapper = new Dictionary <string, object>();

                wrapper.Add("response", response);

                return(base.Serialize(wrapper));
            }
Esempio n. 2
0
 /// <summary>
 /// Serializes to json.
 /// </summary>
 /// <param name="serialization">The serialization.</param>
 /// <returns></returns>
 public override string Serialize(Dictionary <string, object> serialization)
 {
     return(base.Serialize(SerializedResult.BuildResponse(_serializableObject, serialization)));
 }
        /// <summary>
        /// Called when [action executed].
        /// </summary>
        /// <param name="filterContext">The filter context.</param>
        public override void OnActionExecuted(ActionExecutedContext filterContext)
        {
            if (filterContext != null &&
                filterContext.Exception != null &&
                ExceptionResult != null)
            {
                var result = Activator.CreateInstance(ExceptionResult) as ISerializableExceptionResult;
                result.ExceptionMessage = filterContext.Exception.Message;
                result.Exception        = filterContext.Exception;

                if (filterContext.Exception is HttpException)
                {
                    result.ModelSerializer.StatusCode        = (filterContext.Exception as HttpException).GetHttpCode();
                    result.ModelSerializer.StatusDescription = ((HttpStatusCode)result.ModelSerializer.StatusCode).ToString().FromPascalCase();
                }

                filterContext.Result           = result as ActionResult;
                filterContext.ExceptionHandled = true;
            }

            if (filterContext.Result is ViewResult)
            {
                var result = filterContext.Result as ViewResult;
                var view   = result.View as SerializedView;

                switch ((ResponseType)filterContext.RouteData.Values["responseType"])
                {
                case ResponseType.JavaScript:
                    result.View = new JavaScriptCallbackResult();
                    break;

                case ResponseType.Json:
                    result.View = new JsonView();
                    break;

                case ResponseType.Xml:
                    result.View = new XmlView();
                    break;

                case ResponseType.Csv:
                    result.View = new CsvResult();
                    break;

                case ResponseType.Html:
                default:
                    break;
                }

                if (result.View is SerializedView && view != null)
                {
                    ServiceHelper.CopyProperties(view, (SerializedView)result.View);
                }
            }
            else if (filterContext.Result is ISerializableActionResult && !typeof(SerializedResult).IsAssignableFrom(filterContext.Result.GetType()))
            {
                ISerializableActionResult result = filterContext.Result as ISerializableActionResult;

                switch ((ResponseType)filterContext.RouteData.Values["responseType"])
                {
                case ResponseType.JavaScript:
                case ResponseType.Json:
                case ResponseType.Xml:
                case ResponseType.Csv:
                    var resultX = new SerializedResult(result.Model);
                    ServiceHelper.CopyProperties(result.ModelSerializer, resultX.ModelSerializer);
                    break;

                case ResponseType.Html:
                default:
                    filterContext.Result = (ActionResult)result;
                    break;
                }
            }
        }
        /// <summary>
        /// Called when [action executed].
        /// </summary>
        /// <param name="filterContext">The filter context.</param>
        public override void OnActionExecuted(ActionExecutedContext filterContext)
        {
            if (filterContext != null
                && filterContext.Exception != null
                && ExceptionResult != null)
            {
                var result = Activator.CreateInstance(ExceptionResult) as ISerializableExceptionResult;
                result.ExceptionMessage = filterContext.Exception.Message;
                result.Exception = filterContext.Exception;

                if (filterContext.Exception is HttpException)
                {
                    result.ModelSerializer.StatusCode = (filterContext.Exception as HttpException).GetHttpCode();
                    result.ModelSerializer.StatusDescription = ((HttpStatusCode)result.ModelSerializer.StatusCode).ToString().FromPascalCase();
                }

                filterContext.Result = result as ActionResult;
                filterContext.ExceptionHandled = true;
            }

            if (filterContext.Result is ViewResult)
            {
                var result = filterContext.Result as ViewResult;
                var view = result.View as SerializedView;

                switch ((ResponseType)filterContext.RouteData.Values["responseType"])
                {
                    case ResponseType.JavaScript:
                        result.View = new JavaScriptCallbackResult();
                        break;

                    case ResponseType.Json:
                        result.View = new JsonView();
                        break;

                    case ResponseType.Xml:
                        result.View = new XmlView();
                        break;

                    case ResponseType.Csv:
                        result.View = new CsvResult();
                        break;

                    case ResponseType.Html:
                    default:
                        break;
                }

                if (result.View is SerializedView && view != null)
                    ServiceHelper.CopyProperties(view, (SerializedView)result.View);
            }
            else if (filterContext.Result is ISerializableActionResult && !typeof(SerializedResult).IsAssignableFrom(filterContext.Result.GetType()))
            {
                ISerializableActionResult result = filterContext.Result as ISerializableActionResult;

                switch ((ResponseType)filterContext.RouteData.Values["responseType"])
                {
                    case ResponseType.JavaScript:
                    case ResponseType.Json:
                    case ResponseType.Xml:
                    case ResponseType.Csv:
                        var resultX = new SerializedResult(result.Model);
                        ServiceHelper.CopyProperties(result.ModelSerializer, resultX.ModelSerializer);
                        break;

                    case ResponseType.Html:
                    default:
                        filterContext.Result = (ActionResult)result;
                        break;
                }
            }
        }