【发布时间】:2018-06-19 13:19:34
【问题描述】:
我在异常中看不到 CreateErrorResponse 消息。我将消息“无效参数”传递给ThrowResponseException 方法,但我无法在异常中读取它。
e.Response.RequestMessage 给出默认的详细信息,但我只需要阅读“无效参数”。
try
{
If (!ValidString)
{ ThrowResponseException(HttpStatusCode.BadRequest, "Invalid Parameters");}
}
catch (HttpResponseException e)
{
msg = " Status Code: " + e.Response.StatusCode.ToString();
}
private void ThrowResponseException(HttpStatusCode statusCode, string message)
{
var errorResponse = Request.CreateErrorResponse(statusCode,message);
throw new HttpResponseException(errorResponse);
}
【问题讨论】:
-
如果我删除 catch 块,它看起来像它的工作。我将不得不将代码更改为 HttpResponseMessage message = new HttpResponseMessage(statusCode); message.Content = new StringContent(msg);抛出新的 HttpResponseException(message);
标签: c# api asp.net-web-api