Encode() 공개 정적인 메소드

Encodes the default JSON representation of an Error object to a TextWriter.
Only properties and collection entires with non-null and non-empty strings are emitted.
public static Encode ( Error error, TextWriter writer ) : void
error Error
writer System.IO.TextWriter
리턴 void
예제 #1
0
        public static void ProcessRequest(HttpContextBase context)
        {
            var response = context.Response;

            response.ContentType = "application/json";

            //
            // Retrieve the ID of the requested error and read it from
            // the store.
            //

            var errorId = context.Request.QueryString["id"] ?? string.Empty;

            if (errorId.Length == 0)
            {
                throw new ApplicationException("Missing error identifier specification.");
            }

            var entry = ErrorLog.GetDefault(context).GetError(errorId);

            //
            // Perhaps the error has been deleted from the store? Whatever
            // the reason, pretend it does not exist.
            //

            if (entry == null)
            {
                throw new HttpException((int)HttpStatusCode.NotFound,
                                        string.Format("Error with ID '{0}' not found.", errorId));
            }

            //
            // Stream out the error as formatted JSON.
            //

            ErrorJson.Encode(entry.Error, response.Output);
        }
예제 #2
0
            public override void Entries(IList <ErrorLogEntry> entries, int index, int count, int total)
            {
                Debug.Assert(entries != null);
                Debug.Assert(index >= 0);
                Debug.Assert(index + count <= entries.Count);

                StringWriter writer = new StringWriter();

                writer.NewLine = "\n";

                if (_wrapped)
                {
                    writer.WriteLine("<script type='text/javascript' language='javascript'>");
                    writer.WriteLine("//<[!CDATA[");
                }

                writer.Write(_callback);
                writer.Write('(');

                JsonTextWriter json = new JsonTextWriter(writer);

                json.Object()
                .Member("total").Number(total)
                .Member("errors").Array();

                Uri requestUrl = Context.Request.Url;

                for (int i = index; i < count; i++)
                {
                    ErrorLogEntry entry = entries[i];
                    writer.WriteLine();
                    if (i == 0)
                    {
                        writer.Write(' ');
                    }
                    writer.Write("  ");

                    string urlTemplate = new Uri(requestUrl, "{0}?id=" + HttpUtility.UrlEncode(entry.Id)).ToString();

                    json.Object();
                    ErrorJson.Encode(entry.Error, json);
                    json.Member("hrefs")
                    .Array()
                    .Object()
                    .Member("type").String("text/html")
                    .Member("href").String(string.Format(urlTemplate, "detail")).Pop()
                    .Object()
                    .Member("type").String("aplication/json")
                    .Member("href").String(string.Format(urlTemplate, "json")).Pop()
                    .Object()
                    .Member("type").String("application/xml")
                    .Member("href").String(string.Format(urlTemplate, "xml")).Pop()
                    .Pop()
                    .Pop();
                }

                json.Pop();
                json.Pop();

                if (count > 0)
                {
                    writer.WriteLine();
                }

                writer.WriteLine(");");

                if (_wrapped)
                {
                    writer.WriteLine("//]]>");
                    writer.WriteLine("</script>");

                    if (count == 0)
                    {
                        writer.WriteLine(@"</body></html>");
                    }
                }

                Context.Response.Output.Write(writer);
            }
예제 #3
0
            public override IEnumerable <string> Entries(IList <ErrorLogEntry> entries, int index, int count, int total)
            {
                Debug.Assert(entries != null);
                Debug.Assert(index >= 0);
                Debug.Assert(index + count <= entries.Count);

                var writer = new StringWriter {
                    NewLine = "\n"
                };

                if (_wrapped)
                {
                    writer.WriteLine("<script type='text/javascript' language='javascript'>");
                    writer.WriteLine("//<[!CDATA[");
                }

                writer.Write(_callback);
                writer.Write('(');

                var json = new JsonTextWriter(writer);

                json.Object()
                .Member("total").Number(total)
                .Member("errors").Array();

                var requestUrl = ErrorLogPageFactory.GetRequestUrl(Context);

                for (var i = index; i < count; i++)
                {
                    var entry = entries[i];
                    writer.WriteLine();
                    if (i == 0)
                    {
                        writer.Write(' ');
                    }
                    writer.Write("  ");

                    var urlTemplate = new Uri(requestUrl, "{0}?id=" + HttpUtility.UrlEncode(entry.Id)).ToString();

                    json.Object();
                    ErrorJson.Encode(entry.Error, json);
                    json.Member("hrefs")
                    .Array()
                    .Object()
                    .Member("type").String("text/html")
                    .Member("href").String(string.Format(urlTemplate, "detail")).Pop()
                    .Object()
                    .Member("type").String("aplication/json")
                    .Member("href").String(string.Format(urlTemplate, "json")).Pop()
                    .Object()
                    .Member("type").String("application/xml")
                    .Member("href").String(string.Format(urlTemplate, "xml")).Pop()
                    .Pop()
                    .Pop();
                }

                json.Pop();
                json.Pop();

                if (count > 0)
                {
                    writer.WriteLine();
                }

                writer.WriteLine(");");

                if (_wrapped)
                {
                    writer.WriteLine("//]]>");
                    writer.WriteLine("</script>");

                    if (count == 0)
                    {
                        writer.WriteLine(@"</body></html>");
                    }
                }

                yield return(writer.ToString());
            }