[英]How do I send additional Exception data via CreateErrorResponse?
When programming in ASP.NET, and you wish to create HttpResponseMessage from an exception, you can use: 在ASP.NET中进行编程时,如果希望通过异常创建HttpResponseMessage,则可以使用:
var httpResponseMessage =
Request.CreateErrorResponse(HttpStatusCode.InternalServerError, exception);
However, on the browser side, the "responseText" ends up receiving only the following properties of the Exception object: ExceptionMessage, ExceptionType, Message and StackTrace? 但是,在浏览器端,“ responseText”最终仅接收Exception对象的以下属性:ExceptionMessage,ExceptionType,Message和StackTrace?
How can I send additional data? 如何发送其他数据? If I create my own Exception class, or if I add the data to the Exception.Data object.
如果创建自己的Exception类,或者将数据添加到Exception.Data对象。
Instead of passing in the exception directly into CreateErrorResponse, pass in an HttpError object itself that you construct by passing in the exception. 与其直接将异常传递到CreateErrorResponse中,不在于传递通过传递异常而构造的HttpError对象本身。 Then you can add additional properties to the object that's returned to the client.
然后,您可以将其他属性添加到返回给客户端的对象中。
The code you have above is essentially the same as: 您上面的代码与以下代码基本相同:
var httpError = new HttpError(exception, true);
var httpResponseMessage =
Request.CreateErrorResponse(HttpStatusCode.InternalServerError, httpError);
You can call the Add method to httpError to additional fields. 您可以将Add方法添加到httpError的其他字段。 For instance, if you want to add all of the key/values stored in the exception.Data property, your final code would look like this:
例如,如果要添加存储在exception.Data属性中的所有键/值,则最终代码如下所示:
var httpError = new HttpError(exception, true);
foreach (DictionaryEntry entry in exception.Data)
{
httpError.Add(entry.Key.ToString(), entry.Value);
}
var httpResponseMessage =
Request.CreateErrorResponse(HttpStatusCode.InternalServerError, httpError);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.