示例#1
0
        public static void SendError(this HttpResponse response, ushort statusCode, string statusText, string desc)
        {
            if (response == null) throw new ArgumentNullException("response");

            response.StatusCode = statusCode;
            response.StatusText = statusText;

            response.ContentStream = new MemoryStream();

            response.StartHtml("Error!");
            response.AppendHtml("<h1>{0} ({1})</h1>", statusText, statusCode);
            response.AppendHtml("<p>{0}</p>", desc);
            response.EndHtml();

            response.Send();
        }
 public static void Prepend(this TagHelperContent content, IHtmlContent value) {
     if (content.IsEmpty)
         content.SetContent(value);
     else {
         string currentContent = content.GetContent();
         content.SetContent(value);
         content.AppendHtml(currentContent);
     }
 }
        /// <summary>
        /// Writes the specified <paramref name="value"/> with HTML encoding to given <paramref name="content"/>.
        /// </summary>
        /// <param name="content">The <see cref="TagHelperContent"/> to write to.</param>
        /// <param name="encoder">The <see cref="HtmlEncoder"/> to use when encoding <paramref name="value"/>.</param>
        /// <param name="value">The <see cref="object"/> to write.</param>
        /// <returns><paramref name="content"/> after the write operation has completed.</returns>
        /// <remarks>
        /// <paramref name="value"/>s of type <see cref="Html.IHtmlContent"/> are written using
        /// <see cref="Html.IHtmlContent.WriteTo(TextWriter, HtmlEncoder)"/>.
        /// For all other types, the encoded result of <see cref="object.ToString"/>
        /// is written to the <paramref name="content"/>.
        /// </remarks>
        public static TagHelperContent Append(this TagHelperContent content, HtmlEncoder encoder, object value)
        {
            if (content == null)
            {
                throw new ArgumentNullException(nameof(content));
            }

            if (encoder == null)
            {
                throw new ArgumentNullException(nameof(encoder));
            }

            if (value == null)
            {
                // No real action but touch content to ensure IsModified is true.
                content.Append((string)null);
                return content;
            }

            string stringValue;
            var htmlString = value as HtmlEncodedString;
            if (htmlString != null)
            {
                // No need for a StringWriter in this case.
                stringValue = htmlString.ToString();
            }
            else
            {
                using (var stringWriter = new StringWriter())
                {
                    RazorPage.WriteTo(stringWriter, encoder, value);
                    stringValue = stringWriter.ToString();
                }
            }

            // In this case the text likely came directly from the Razor source. Since the original string is
            // an attribute value that may have been quoted with single quotes, must handle any double quotes
            // in the value. Writing the value out surrounded by double quotes.
            content.AppendHtml(stringValue.Replace("\"", "&quot;"));

            return content;
        }
        /// <summary>
        /// Appends the specified <paramref name="format"/> to the existing content after replacing each format
        /// item with the HTML encoded <see cref="string"/> representation of the corresponding item in the
        /// <paramref name="args"/> array.
        /// </summary>
        /// <param name="builder">The <see cref="IHtmlContentBuilder"/>.</param>
        /// <param name="format">
        /// The composite format <see cref="string"/> (see http://msdn.microsoft.com/en-us/library/txafckwd.aspx).
        /// The format string is assumed to be HTML encoded as-provided, and no further encoding will be performed.
        /// </param>
        /// <param name="args">
        /// The object array to format. Each element in the array will be formatted and then HTML encoded.
        /// </param>
        /// <returns>A reference to this instance after the append operation has completed.</returns>
        public static IHtmlContentBuilder AppendFormat(
            this IHtmlContentBuilder builder,
            string format,
            params object[] args)
        {
            if (builder == null)
            {
                throw new ArgumentNullException(nameof(builder));
            }

            if (format == null)
            {
                throw new ArgumentNullException(nameof(format));
            }

            if (args == null)
            {
                throw new ArgumentNullException(nameof(args));
            }

            builder.AppendHtml(new HtmlFormattableString(format, args));
            return builder;
        }
 /// <summary>
 /// Sets the content to the <see cref="string"/> value. The value is treated as HTML encoded as-provided, and
 /// no further encoding will be performed.
 /// </summary>
 /// <param name="builder">The <see cref="IHtmlContentBuilder"/>.</param>
 /// <param name="encoded">The HTML encoded <see cref="string"/> that replaces the content.</param>
 /// <returns>The <see cref="IHtmlContentBuilder"/>.</returns>
 public static IHtmlContentBuilder SetHtmlContent(this IHtmlContentBuilder builder, string encoded)
 {
     builder.Clear();
     builder.AppendHtml(encoded);
     return builder;
 }
 /// <summary>
 /// Appends an <see cref="Environment.NewLine"/> after appending the <see cref="string"/> value.
 /// The value is treated as HTML encoded as-provided, and no further encoding will be performed.
 /// </summary>
 /// <param name="builder">The <see cref="IHtmlContentBuilder"/>.</param>
 /// <param name="encoded">The HTML encoded <see cref="string"/> to append.</param>
 /// <returns>The <see cref="IHtmlContentBuilder"/>.</returns>
 public static IHtmlContentBuilder AppendHtmlLine(this IHtmlContentBuilder builder, string encoded)
 {
     builder.AppendHtml(encoded);
     builder.Append(HtmlEncodedString.NewLine);
     return builder;
 }
        /// <summary>
        /// Sets the content to the <see cref="string"/> value. The value is treated as HTML encoded as-provided, and
        /// no further encoding will be performed.
        /// </summary>
        /// <param name="builder">The <see cref="IHtmlContentBuilder"/>.</param>
        /// <param name="encoded">The HTML encoded <see cref="string"/> that replaces the content.</param>
        /// <returns>The <see cref="IHtmlContentBuilder"/>.</returns>
        public static IHtmlContentBuilder SetHtmlContent(this IHtmlContentBuilder builder, string encoded)
        {
            if (builder == null)
            {
                throw new ArgumentNullException(nameof(builder));
            }

            builder.Clear();
            builder.AppendHtml(encoded);
            return builder;
        }
        /// <summary>
        /// Appends an <see cref="Environment.NewLine"/> after appending the <see cref="IHtmlContent"/> value.
        /// </summary>
        /// <param name="builder">The <see cref="IHtmlContentBuilder"/>.</param>
        /// <param name="content">The <see cref="IHtmlContent"/> to append.</param>
        /// <returns>The <see cref="IHtmlContentBuilder"/>.</returns>
        public static IHtmlContentBuilder AppendLine(this IHtmlContentBuilder builder, IHtmlContent content)
        {
            if (builder == null)
            {
                throw new ArgumentNullException(nameof(builder));
            }

            builder.AppendHtml(content);
            builder.AppendHtml(HtmlString.NewLine);
            return builder;
        }