Using c# Web Api 2, I have code that throws an InvalidOperationException
. When returning a status code of 302, how do provide a location for the redirect using the HandleException
annotation?
[HandleException(typeof(InvalidOperationException), HttpStatusCode.Found, ResponseContent = "Custom message 12")]
public IHttpActionResult GetHandleException(int num)
{
switch (num)
{
case 12: throw new InvalidOperationException("DONT SHOW invalid operation exception");
default: throw new Exception("base exception");
}
}
Edit: Sorry, I asked this question in a bit of haste. The above class uses a HandleExceptionAttribute class which inherits from ExceptionFilterAttribute. I didn't realize this at the time I was trying to debug their unit test. The problem doesn't arise in a unit test, but does show up using a Visual Studio .webtest that requires the redirect url. The class that inherits from ExceptionFilterAttribute did not provide a parameter that allows for the redirected URL to be supplied. Sorry for an incomplete question and thanks for taking time to answer.
/// <summary>
/// This attribute will handle exceptions thrown by an action method in a consistent way
/// by mapping an exception type to the desired HTTP status code in the response.
/// It may be applied multiple times to the same method.
/// </summary>
[AttributeUsage(AttributeTargets.Method, Inherited = false, AllowMultiple = true)]
public sealed class HandleExceptionAttribute : ExceptionFilterAttribute
{