C# Postsharp:异常后如何设置返回值
使用Postsharp,在抛出异常后如何设置返回值 我认为这会奏效:C# Postsharp:异常后如何设置返回值,c#,postsharp,C#,Postsharp,使用Postsharp,在抛出异常后如何设置返回值 我认为这会奏效: namespace MvcApplication3.Controllers { public class ValuesController : ApiController { // GET api/values/5 [MyExceptionHandler] public string Get(int id) { string
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";
}