示例#1
0
        public static void WriteErrorDetailsResponseJson(this HttpContext Context, ErrorDetails errorDetails)
        {
            Context.Response.ContentType = "application/json";
            using Utf8JsonWriter writer  = new System.Text.Json.Utf8JsonWriter(Context.Response.BodyWriter);
            writer.WriteStartObject();
            foreach (var item in errorDetails.GetType().GetProperties())
            {
                writer.WritePropertyName(item.Name);
                if (item.PropertyType == typeof(int))
                {
                    writer.WriteNumberValue((int)item.GetValue(errorDetails));
                }
                else if (item.PropertyType == typeof(string))
                {
                    writer.WriteStringValue(item.GetValue(errorDetails).ToString());
                }
            }

            writer.WriteEndObject();
            writer.Flush();
        }