示例#1
0
        private HtmlString SerializeInternal(JsonOutputFormatter jsonOutputFormatter, object value)
        {
            var stringWriter = new StringWriter(CultureInfo.InvariantCulture);
            jsonOutputFormatter.WriteObject(stringWriter, value);

            return new HtmlString(stringWriter.ToString());
        }
示例#2
0
 protected override void PopulateStoreConfig(IDictionary <string, object> config)
 {
     using (var writer = new StringWriter()) {
         _jsonFormatter.WriteObject(writer, Items);
         config["data"] = new JRaw(writer.ToString());
     }
 }
示例#3
0
        public override async Task WriteResponseBodyAsync(OutputFormatterWriteContext context, Encoding selectedEncoding)
        {
            if (context == null)
            {
                throw new ArgumentNullException(nameof(context));
            }

            string callback;

            if (IsJsonpRequest(context.HttpContext.Request, _callbackQueryParameter, out callback))
            {
                if (!CallbackValidator.IsValid(callback))
                {
                    throw new InvalidOperationException($"Callback '{callback}' is invalid!");
                }

                using (var writer = context.WriterFactory(context.HttpContext.Response.Body, selectedEncoding))
                {
                    // the /**/ is a specific security mitigation for "Rosetta Flash JSONP abuse"
                    // the typeof check is just to reduce client error noise
                    var str = "/**/ typeof " + callback + " === 'function' && " + callback + "(";
                    str += context.Object + ");";
                    writer.Write("/**/ typeof " + callback + " === 'function' && " + callback + "(");
                    writer.Flush();
                    _jsonMediaTypeFormatter.WriteObject(writer, context.Object);
                    writer.Write(");");
                    await writer.FlushAsync();
                }
            }
            else
            {
                await _jsonMediaTypeFormatter.WriteResponseBodyAsync(context, selectedEncoding);
            }
        }
示例#4
0
        private IHtmlContent SerializeInternal(JsonOutputFormatter jsonOutputFormatter, object value)
        {
            var stringWriter = new StringWriter(CultureInfo.InvariantCulture);

            jsonOutputFormatter.WriteObject(stringWriter, value);

            return(new HtmlString(stringWriter.ToString()));
        }
示例#5
0
        public async Task CacheResult(string cacheKey, ApiResult result,
                                      int expire = 0)
        {
            if (cacheKey == null)
            {
                throw new ArgumentNullException(nameof(cacheKey));
            }
            if (_connection == null)
            {
                return;
            }

            // Store the IActionResult object as a JSON string
            using (var writer = new StringWriter())
            {
                _outputFormatter.WriteObject(writer, result);
                string storedValue = writer.ToString();
                await _connection.GetDatabase().StringSetAsync(
                    cacheKey, storedValue, expiry: TimeSpan.FromSeconds(expire)
                    );
            }
        }