使用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";
}
}
}
Run Code Online (Sandbox Code Playgroud)
我想到了.
你需要从函数返回,而不是继续.
public override void OnException(MethodExecutionArgs args)
{
args.FlowBehavior = FlowBehavior.Return;
args.ReturnValue = "Error Getting Value";
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
788 次 |
| 最近记录: |