ModelClass是个模型类
public ModelClass Get(){ Service _Service =new Service (); ModelClass list= _Service.getAll(); if(list!=null){ return list; } else { return ???; } }
在不修改或继承ModelClass的情况下,这样的请求如何在list为null的时候返回其他类型的值呢?比如自定义字符串“Error”。
asp.net网站上有篇Exception 处理的文章使用的这个方法:
throw new HttpResponseException(HttpStatusCode.NotFound);
但是直接使用throw会造成程序报错啊,如果处理了这个异常又不会触发了
刚把asp.net上的文章仔细看了遍,可以使用HttpResponseMessage作为返回对象,当成功的时候使用return Request.CreateResponse(HttpStatusCode.OK, list)来返回值,不成功则可以依自己需求返回不同状态码及不同类型的数据,CreateResponse共8个重载方法
get(out string errMessage)
应该是抛出一个自定义的异常,然后用HandleErrorAttribute或者Controller自己的OnException来处理这个异常