我正在C#上创建API。 当我遇到异常时,我会尝试发回一条帮助信息,但是 “远程服务器返回错误:(500)内部服务器错误。”替代原始的。
服务器端的代码是
string authHeader = actionContext.Request.Headers.Authorization.Parameter; if (authHeader.Equals(ATOKEN) || authHeader.Equals(BTOKEN)) { //goal reached. Token Authentication is valid. } else { throw new HttpException("Invalid User!"); }
当我尝试在客户端处理异常时,a找不到消息“无效用户”!
对此有什么建议吗?
宁可使用 HttpResponseException 如下所示
HttpResponseException
var resp = new HttpResponseMessage(HttpStatusCode.NotFound) { ReasonPhrase = "Invalid User!" }; throw new HttpResponseException(resp);