继承Exception

it2026-09-29  7

1 public class ECOrderException : Exception 2 { //第一种类型: throw new ECOrderException { ErrorCode = "X1008-06", TransMessage = "SNs格式错误" }; 3 public ECOrderException() : base() 4 { 6 } 8 public virtual string ErrorCode { get; set; } 9 public virtual string TransMessage { get; set; } 10 //第二种类型:throw new ECOrderException("CO,WHSE 等栏位不能为空"); 11 //public ECOrderException(string Message): base(Message) 13 //{ 14 // TransMessage = Message; 15 //} 16 }

上面,有两个虚拟字段Error_Code和TransMessage

使用的时候,可以这样使用:

throw new ECOrderException { ErrorCode = "X1008-01", TransMessage = "交易代码验证失败" }; var obj = new JObject( new JProperty("head", new JObject( new JProperty("transMessage", transMessage), new JProperty("errorCode", ErrorCode) )), new JProperty("body", new JObject()) ); var result = new ContentResult() { Content = Newtonsoft.Json.JsonConvert.SerializeObject(obj), ContentType = "application/json" };

然后这样就可以返回一个Json的格式信息给前端。

转载于:https://www.cnblogs.com/ZkbFighting/p/11249102.html

最新回复(0)