如何为302状态代码使用handleexeption批注时如何指定位置

The*_*tan 7 c# asp.net-mvc visual-studio asp.net-web-api2 webapi2

使用c#Web Api 2,我有抛出的代码InvalidOperationException.返回状态代码302时,如何使用HandleException注释为重定向提供位置?

[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");
     }
}
Run Code Online (Sandbox Code Playgroud)

编辑:对不起,我有点急忙问这个问题.上面的类使用HandleExceptionAttribute类,该类继承自ExceptionFilterAttribute.当我试图调试他们的单元测试时,我没有意识到这一点.单元测试中不会出现此问题,但使用需要重定向URL的Visual Studio .webtest会显示该问题.从ExceptionFilterAttribute继承的类未提供允许提供重定向URL的参数.对不起,问题不完整,谢谢你花时间回答.

/// <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
   {
Run Code Online (Sandbox Code Playgroud)

And*_* M. 3

编辑:感谢您更新问题。尽管我仍然不确定为什么要在此 WebApi 方法中进行重定向。希望这个答案能有所帮助。

我将处理 HandleExceptionAttribute 中的所有异常逻辑。您甚至可以使用您正在寻找的 302 代码从那里进行重定向。你的 HandleExceptionAttribute 看起来像这样(我提供了 3 种不同的基于异常返回结果的方法):

public sealed class HandleExceptionAttribute : ExceptionFilterAttribute
{
    public override void OnException(HttpActionExecutedContext context)
    {
        //TODO: we can handle all types of exceptions here. Out of memory, divide by zero, etc etc.
        if (context.Exception is InvalidOperationException)
        {
            var httpResponseMessage = context.Request.CreateResponse(HttpStatusCode.Redirect);
            httpResponseMessage.Headers.Location = new Uri("http://www.YourRedirectUrl");
            throw new HttpResponseException(httpResponseMessage);
        }
        if (context.Exception is UnauthorizedAccessException)
        {
            context.Response = context.Request.CreateErrorResponse(HttpStatusCode.Unauthorized, context.Exception.Message);
            return;
        }
        if (context.Exception is TimeoutException)
        {
            throw new HttpResponseException(context.Request.CreateResponse(HttpStatusCode.RequestTimeout, context.Exception.Message));
        }

        context.Response = context.Request.CreateErrorResponse(HttpStatusCode.InternalServerError, "Unable to process your request.");
    }
}
Run Code Online (Sandbox Code Playgroud)

但是,如果您确实希望按照您要求的方式完成,您可以向 GetHandleException 方法添加第二个参数。这将接收消息字符串(或 URL),然后在 HandleExceptionAttribute 中将重定向 url 添加到参数(ActionArguements):

public IHttpActionResult GetHandleException(int num, string message = "")
{
    switch (num)
    {
        case 12: return Redirect(message); //message string would be your url

        default: throw new Exception("base exception");
    }
}
Run Code Online (Sandbox Code Playgroud)

那么你的 HandleExceptionAttribute 看起来像这样:

public sealed class HandleExceptionAttribute : ExceptionFilterAttribute
{
    public override void OnException(HttpActionExecutedContext context)
    {
        context.ActionContext.ActionArguments["message"] = "your redirect URL";

    }
}
Run Code Online (Sandbox Code Playgroud)