WebApi2 IHttpActionResult strongly typed return values

后端 未结 4 1469
借酒劲吻你
借酒劲吻你 2021-01-18 16:10

It is possible and not ideal to do this: (a vastly simplified example!)

[Serializable]
public class MyRecord
{
    public string key {get; set;}
    public          


        
4条回答
  •  感动是毒
    2021-01-18 16:21

    You can refactor your code this way:

    public class Answer
    {
        public T result {get;set;}
        public bool success {get;set;}
        public string exception {get;set;}
    }
    
    public async Task> Get(string SomeKey)
    {
        var answer = new Answer();
        try
        {     
            if(ExistsInDB(SomeKey))
            {
                answer.result = await SomeRecordFromDB(SomeKey);
                answer.success = true;
            }
        }
        catch(Exception e)
        {
            answer.exception = e.Message;            
        }
        return answer;
    }
    

提交回复
热议问题