c# - Postsharp:如何设置异常后的返回值

标签 c# postsharp

使用 Postsharp,如何设置抛出异常后的返回值?

我认为这会起作用:

namespace MvcApplication3.Controllers
{
    public class ValuesController : ApiController
    {
        // GET api/values/5
        [MyExceptionHandler]
        public string Get(int id)
        {
            string value = GetValue(id);
            return value;
        }

        private string GetValue(int id)
        {
            throw new DivideByZeroException();
        }
    }

    [Serializable]
    public class MyExceptionHandler : OnExceptionAspect
    {
        public override void OnException(MethodExecutionArgs args)
        {
            args.FlowBehavior = FlowBehavior.Continue;
            args.ReturnValue = "Error Getting Value";
        }
    }
}

最佳答案

我想通了。

您需要从函数中返回,而不是继续。

    public override void OnException(MethodExecutionArgs args)
    {
        args.FlowBehavior = FlowBehavior.Return;
        args.ReturnValue = "Error Getting Value";
    }

关于c# - Postsharp:如何设置异常后的返回值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21898956/

相关文章:

c# - 后锐利。如何引入可序列化属性

窗口打开时 WPF 绑定(bind)不正确

c# - 如果集合尚未通过比较项目的属性将项目添加到集合中?

c# - Entity Framework - 检查 foreach/update 查询

c# - 使用平面样式更改 ToolStripComboBox 的边框

c# - Winforms 中的可视 TreeMap

c# - ReSharper 错误地声称 IDictionary.Values 总是非空的

performance - Windows Azure 中的自定义性能计数器

c# - 用于 PostSharp 2.0 和 .NET Framework 2.0 的 Log4PostSharp

c# - 如何在循环外访问字符串数组