ASP.NET MVC Web API – JObject / JArray – JSON Exception 方法
\Library\JException.cs |
using System; using Newtonsoft.Json.Linq; namespace SoccerWebAPIODAC.Library { public class JException { public static JArray JArrayException(int ErrorCode, string Message) { JArray ary = new JArray(); ary.Add(new JObject( new JProperty("Code", ErrorCode), new JProperty("Exception", Message))); return ary; } public static JObject JObjectException(int ErrorCode, string Message) { JObject obj = new JObject(); obj.Add("Code", ErrorCode); obj.Add("Exception", Message); return obj; } } } |
\Controllers\JPlayerController.cs |
using System; using System.Web.Http; using SoccerWebAPIODAC.Library; using SoccerDAO.DAO; using Newtonsoft.Json.Linq; namespace SoccerWebAPIODAC.Controllers { public class PlayerController : SoccerWebAPIHelper { [Route("rest/player/nation/{id}")] public JArray GetNation(int id) { try { return this.playerDAO.getPlayerByNation(id); } catch(Exception ex) { return JException.JArrayException(1, ex.Message); } } … … } } |