using System; using System.Net; using EmbedIO.Utilities; namespace EmbedIO { partial class HttpContextExtensions { /// /// Sets a redirection status code and adds a Location header to the response. /// /// The interface on which this method is called. /// The URL to which the user agent should be redirected. /// The status code to set on the response. /// is . /// is . /// /// is not a valid relative or absolute URL.. /// - or - /// is not a redirection (3xx) status code. /// public static void Redirect(this IHttpContext @this, string location, int statusCode = (int)HttpStatusCode.Found) { location = Validate.Url(nameof(location), location, @this.Request.Url); if (statusCode < 300 || statusCode > 399) throw new ArgumentException("Redirect status code is not valid.", nameof(statusCode)); @this.Response.SetEmptyResponse(statusCode); @this.Response.Headers[HttpHeaderNames.Location] = location; } } }