示例#1
0
        /// <summary>
        /// Prepares a standard response without a body for the specified status code.
        /// </summary>
        /// <param name="this">The <see cref="IHttpResponse"/> interface on which this method is called.</param>
        /// <param name="statusCode">The HTTP status code of the response.</param>
        /// <exception cref="NullReferenceException"><paramref name="this"/> is <see langword="null"/>.</exception>
        /// <exception cref="ArgumentException">There is no standard status description for <paramref name="statusCode"/>.</exception>
        public static void SetEmptyResponse(this IHttpResponse @this, int statusCode)
        {
            if (!HttpStatusDescription.TryGet(statusCode, out var statusDescription))
            {
                throw new ArgumentException("Status code has no standard description.", nameof(statusCode));
            }

            @this.StatusCode        = statusCode;
            @this.StatusDescription = statusDescription;
            @this.ContentType       = string.Empty;
            @this.ContentEncoding   = null;
        }
        /// <summary>
        /// Asynchronously sends a standard HTML response for the specified status code.
        /// </summary>
        /// <param name="this">The <see cref="IHttpContext"/> interface on which this method is called.</param>
        /// <param name="statusCode">The HTTP status code of the response.</param>
        /// <param name="writeAdditionalHtml">A callback function that may write additional HTML code
        /// to a <see cref="TextWriter"/> representing the response output.
        /// If not <see langword="null"/>, the callback is called immediately before closing the HTML <c>body</c> tag.</param>
        /// <returns>A <see cref="Task"/> representing the ongoing operation.</returns>
        /// <exception cref="NullReferenceException"><paramref name="this"/> is <see langword="null"/>.</exception>
        /// <exception cref="ArgumentException">There is no standard status description for <paramref name="statusCode"/>.</exception>
        /// <seealso cref="SendStandardHtmlAsync(IHttpContext,int)"/>
        public static Task SendStandardHtmlAsync(
            this IHttpContext @this,
            int statusCode,
            Action <TextWriter> writeAdditionalHtml)
        {
            if (!HttpStatusDescription.TryGet(statusCode, out var statusDescription))
            {
                throw new ArgumentException("Status code has no standard description.", nameof(statusCode));
            }

            @this.Response.StatusCode        = statusCode;
            @this.Response.StatusDescription = statusDescription;
            @this.Response.ContentType       = MimeType.Html;
            @this.Response.ContentEncoding   = Encoding.UTF8;
            using (var text = @this.OpenResponseText(Encoding.UTF8))
            {
                text.Write(StandardHtmlHeaderFormat, statusCode, statusDescription, Encoding.UTF8.WebName);
                writeAdditionalHtml?.Invoke(text);
                text.Write(StandardHtmlFooter);
            }

            return(Task.CompletedTask);
        }