Ejemplo n.º 1
0
        public override void ExecuteResult(ControllerContext context)
        {
            Guard.ArgumentNotNull(() => context);

            if (this.Data == null)
            {
                return;
            }

            if (this.JsonRequestBehavior == JsonRequestBehavior.DenyGet && context.HttpContext.Request.HttpMethod.IsCaseInsensitiveEqual("GET"))
            {
                throw new InvalidOperationException("This request has been blocked because sensitive information could be disclosed to third party web sites when this is used in a GET request.To allow GET requests, set JsonRequestBehavior to AllowGet.");
            }

            var response = context.HttpContext.Response;

            if (this.ContentEncoding != null)
            {
                response.ContentEncoding = this.ContentEncoding;
            }

            response.ContentType = this.ContentType.NullEmpty() ?? "application/json";

            var serializerSettings = _settings ?? new JsonSerializerSettings
            {
                MissingMemberHandling = MissingMemberHandling.Ignore,

                // Limit the object graph we'll consume to a fixed depth. This prevents stackoverflow exceptions
                // from deserialization errors that might occur from deeply nested objects.
                MaxDepth = 32,

                // Do not change this setting
                // Setting this to None prevents Json.NET from loading malicious, unsafe, or security-sensitive types
                TypeNameHandling = TypeNameHandling.None
            };

            if (_settings == null)
            {
                serializerSettings.DateTimeZoneHandling = DateTimeZoneHandling.RoundtripKind;
                serializerSettings.DateFormatHandling   = DateFormatHandling.MicrosoftDateFormat;

                var utcDateTimeConverter = new UTCDateTimeConverter(_dateTimeHelper, new JavaScriptDateTimeConverter());
                serializerSettings.Converters.Add(utcDateTimeConverter);
            }

            using (var jsonWriter = new JsonTextWriter(response.Output))
            {
                jsonWriter.CloseOutput = false;
                var jsonSerializer = JsonSerializer.Create(serializerSettings);
                jsonSerializer.Serialize(jsonWriter, this.Data);
            }
        }
Ejemplo n.º 2
0
        public override void ExecuteResult(ControllerContext context)
        {
            Guard.NotNull(context, nameof(context));

            if (this.Data == null)
            {
                return;
            }

            if (this.JsonRequestBehavior == JsonRequestBehavior.DenyGet && context.HttpContext.Request.HttpMethod.IsCaseInsensitiveEqual("GET"))
            {
                throw new InvalidOperationException("This request has been blocked because sensitive information could be disclosed to third party web sites when this is used in a GET request.To allow GET requests, set JsonRequestBehavior to AllowGet.");
            }

            var response = context.HttpContext.Response;

            if (this.ContentEncoding != null)
            {
                response.ContentEncoding = this.ContentEncoding;
            }

            response.ContentType = this.ContentType.NullEmpty() ?? "application/json";

            var serializerSettings = _settings ?? CreateDefaultSerializerSettings();

            if (_settings == null)
            {
                var utcDateTimeConverter = new UTCDateTimeConverter(_dateTimeHelper, new JavaScriptDateTimeConverter());
                serializerSettings.Converters.Add(utcDateTimeConverter);
            }

            using (var jsonWriter = new JsonTextWriter(response.Output))
            {
                jsonWriter.CloseOutput = false;
                var jsonSerializer = JsonSerializer.Create(serializerSettings);
                jsonSerializer.Serialize(jsonWriter, this.Data);
            }
        }