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

前端之家收集整理的这篇文章主要介绍了c# – 如何为302状态代码使用handleexeption批注时指定位置前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
使用c#Web Api 2,我有抛出InvalidOperationException的代码.返回状态代码302时,如何使用HandleException批注为重定向提供位置?
  1. [HandleException(typeof(InvalidOperationException),HttpStatusCode.Found,ResponseContent = "Custom message 12")]
  2. public IHttpActionResult GetHandleException(int num)
  3. {
  4. switch (num)
  5. {
  6. case 12: throw new InvalidOperationException("DONT SHOW invalid operation exception");
  7.  
  8. default: throw new Exception("base exception");
  9. }
  10. }

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

  1. /// <summary>
  2. /// This attribute will handle exceptions thrown by an action method in a consistent way
  3. /// by mapping an exception type to the desired HTTP status code in the response.
  4. /// It may be applied multiple times to the same method.
  5. /// </summary>
  6. [AttributeUsage(AttributeTargets.Method,Inherited = false,AllowMultiple = true)]
  7. public sealed class HandleExceptionAttribute : ExceptionFilterAttribute
  8. {

解决方法

编辑:感谢您更新了问题.虽然我仍然不确定你为什么要在这个WebApi方法重定向.希望这个答案可以提供帮助.

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

  1. public sealed class HandleExceptionAttribute : ExceptionFilterAttribute
  2. {
  3. public override void OnException(HttpActionExecutedContext context)
  4. {
  5. //TODO: we can handle all types of exceptions here. Out of memory,divide by zero,etc etc.
  6. if (context.Exception is InvalidOperationException)
  7. {
  8. var httpResponseMessage = context.Request.CreateResponse(HttpStatusCode.Redirect);
  9. httpResponseMessage.Headers.Location = new Uri("http://www.YourRedirectUrl");
  10. throw new HttpResponseException(httpResponseMessage);
  11. }
  12. if (context.Exception is UnauthorizedAccessException)
  13. {
  14. context.Response = context.Request.CreateErrorResponse(HttpStatusCode.Unauthorized,context.Exception.Message);
  15. return;
  16. }
  17. if (context.Exception is TimeoutException)
  18. {
  19. throw new HttpResponseException(context.Request.CreateResponse(HttpStatusCode.RequestTimeout,context.Exception.Message));
  20. }
  21.  
  22. context.Response = context.Request.CreateErrorResponse(HttpStatusCode.InternalServerError,"Unable to process your request.");
  23. }
  24. }

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

  1. public IHttpActionResult GetHandleException(int num,string message = "")
  2. {
  3. switch (num)
  4. {
  5. case 12: return Redirect(message); //message string would be your url
  6.  
  7. default: throw new Exception("base exception");
  8. }
  9. }

然后你的HandleExceptionAttribute看起来像这样:

  1. public sealed class HandleExceptionAttribute : ExceptionFilterAttribute
  2. {
  3. public override void OnException(HttpActionExecutedContext context)
  4. {
  5. context.ActionContext.ActionArguments["message"] = "your redirect URL";
  6.  
  7. }
  8. }

猜你在找的C#相关文章