Skip to main content

Extension methods for the HttpResponseExtensions class.

using System.Web;

namespace Extensions {

    public static class HttpResponseExtensions {

        /// <summary>
        /// Reloads the current page / handler by performing a redirect to the current url
        /// </summary>
        /// <param name="self">The HttpResponse to perform on.</param>
        public static void Reload(this HttpResponse self)
        {
            self.Redirect(HttpContext.Current.Request.Url.ToString(), true);
        }

        /// <summary>
        /// Performs a response redirect and allows the url to be populated
        /// with string format parameters.
        /// </summary>
        /// <param name="self">The HttpResponse to perform on.</param>
        /// <param name="urlFormat">The URL including string.Format placeholders.</param>
        /// <param name="values">The values to the populated.</param>
        public static void Redirect(this HttpResponse self, string urlFormat, params object[] values)
        {
            self.Redirect(urlFormat, true, values);
        }

        /// <summary>
        /// Performs a response redirect and allows the url to be populated
        /// with string format parameters.
        /// </summary>
        /// <param name="self">The HttpResponse to perform on.</param>
        /// <param name="urlFormat">The URL including string.Format
        /// placeholders.
        /// </param>
        /// <param name="endResponse">If set to <c>true</c> the response
        /// will be terminated.
        /// </param>
        /// <param name="values">The values to the populated.</param>
        public static void Redirect(this HttpResponse self, string urlFormat, bool endResponse, params object[] values)
        {
            var url = string.Format(urlFormat, values);
            self.Redirect(url, endResponse);
        }

        /// <summary>
        /// Performs a response redirect and allows the url to be populated
        /// with a query string.
        /// </summary>
        /// <param name="self">The HttpResponse to perform on.</param>
        /// <param name="url">The URL.</param>
        /// <param name="queryString">The query string.</param>
        public static void Redirect(this HttpResponse self, string url, UriQueryString queryString)
        {
            self.Redirect(url, queryString, true);
        }

        /// <summary>
        /// Performs a response redirect and allows the url to be populated
        /// with a query string.
        /// </summary>
        /// <param name="self">The HttpResponse to perform on.</param>
        /// <param name="url">The URL.</param>
        /// <param name="queryString">The query string.</param>
        /// <param name="endResponse">If set to <c>true</c> the response will
        /// be terminated.
        /// </param>
        public static void Redirect(this HttpResponse self, string url, UriQueryString queryString, bool endResponse)
        {
            url = queryString.ToString(url);
            self.Redirect(url, endResponse);
        }

        /// <summary>
        /// Returns a 404 to the client and ends the response.
        /// </summary>
        /// <param name="self">The HttpResponse to perform on.</param>
        public static void SetFileNotFound(this HttpResponse self)
        {
            self.SetFileNotFound(true);
        }

        /// <summary>
        /// Returns a 404 to the client and optionally ends the response.
        /// </summary>
        /// <param name="self">The HttpResponse to perform on.</param>
        /// <param name="endResponse">If set to <c>true</c> the response will
        /// be terminated.
        /// </param>
        public static void SetFileNotFound(this HttpResponse self, bool endResponse)
        {
            self.SetStatus(404, "Not Found", endResponse);
        }

        /// <summary>
        /// Returns a 500 to the client and ends the response.
        /// </summary>
        /// <param name="self">The HttpResponse to perform on.</param>
        public static void SetInternalServerError(this HttpResponse self)
        {
            self.SetInternalServerError(true);
        }

        /// <summary>
        /// Returns a 500 to the client and optionally ends the response.
        /// </summary>
        /// <param name="self">The HttpResponse to perform on.</param>
        /// <param name="endResponse">If set to <c>true</c> the response
        /// will be terminated.
        /// </param>
        public static void SetInternalServerError(this HttpResponse self, bool endResponse)
        {
            self.SetStatus(500, "Internal Server Error", endResponse);
        }

        /// <summary>
        /// Set the specified HTTP status code and description and optionally
        /// ends the response.
        /// </summary>
        /// <param name="self">The HttpResponse to perform on.</param>
        /// <param name="code">The status code.</param>
        /// <param name="description">The status description.</param>
        /// <param name="endResponse">If set to <c>true</c> the response
        /// will be terminated.
        /// </param>
        public static void SetStatus(this HttpResponse self, int code, string description, bool endResponse)
        {
            self.StatusCode = code;
            self.StatusDescription = description;

            if (endResponse)
            {
                self.End();
            }
        }

    }
}