简体   繁体   English

如何在异常中读取CreateErrorResponse自定义消息

[英]How to read CreateErrorResponse custom message in exception

I can't see CreateErrorResponse message in exception. 我无法在异常中看到CreateErrorResponse消息。 I am passing message "Invalid Parameters" to ThrowResponseException method but i can't read it in exception. 我正在将消息“无效参数”传递给ThrowResponseException方法,但我无法在异常中读取它。

e.Response.RequestMessage gives default detail message but i only need to read "Invalid Parameters". 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);
}

I found something similar where they wrap the message in a new exception and pass that exception into the create response. 我发现类似的地方,他们将消息包装在新的异常中,然后将该异常传递到create响应中。 Then they can pull out the text of the error message. 然后他们可以拉出错误消息的文本。 How can I retrieve the exception message from Request.CreateErrorResponse? 如何从Request.CreateErrorResponse中检索异常消息?

return Request.CreateResponse(statusCode, new Exception(message));

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM