using System;
using System.Net;
namespace EmbedIO
{
///
/// When thrown, breaks the request handling control flow
/// and sends a redirection response to the client.
///
#pragma warning disable CA1032 // Implement standard exception constructors - they have no meaning here.
public class HttpRedirectException : HttpException
#pragma warning restore CA1032
{
///
/// Initializes a new instance of the class.
///
/// The redirection target.
///
/// The status code to set on the response, in the range from 300 to 399.
/// By default, status code 302 (Found) is used.
///
/// is not in the 300-399 range.
public HttpRedirectException(string location, int statusCode = (int)HttpStatusCode.Found)
: base(statusCode)
{
if (statusCode < 300 || statusCode > 399)
throw new ArgumentException("Redirect status code is not valid.", nameof(statusCode));
Location = location;
}
///
/// Initializes a new instance of the class.
///
/// The redirection target.
/// One of the redirection status codes, to be set on the response.
/// is not a redirection status code.
public HttpRedirectException(string location, HttpStatusCode statusCode)
: this(location, (int)statusCode)
{
}
///
/// Gets the URL where the client will be redirected.
///
public string Location { get; }
///
public override void PrepareResponse(IHttpContext context)
{
context.Redirect(Location, StatusCode);
}
}
}