使用 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/