简体   繁体   English

如何通过CreateErrorResponse发送其他异常数据?

[英]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.

相关问题 如何从Request.CreateErrorResponse检索异常消息? - How can I retrieve the exception message from Request.CreateErrorResponse? 如何在异常中读取CreateErrorResponse自定义消息 - How to read CreateErrorResponse custom message in exception 如何将其他异常数据传递给C#中的全局异常过滤器? - How do I pass additional exception data to a global exception filter in C#? 如何将其他数据传递到ValidationRule中? - How do I pass additional data into a ValidationRule? 如何通过curl将数据发送到mvc4 Web服务 - How do I send a data via curl into an mvc4 web service 如何检测通过TLS安全套接字发送数据的尝试失败? - How do I detect that my attempt to send data via TLS secured socket has failed? 如何通过 ajax 将包含表单数据但不单击提交按钮的视图 model 发送到 controller - How do I send a view model to the controller via ajax which includes form data but not by clicking the submit button CreateErrorResponse不会覆盖异常堆栈跟踪和消息 - CreateErrorResponse does not override Exception stacktrace and message 如何存储其他数据或将其与PasswordCredential相关联? - How do I store or associate additional data with a PasswordCredential? 具有CreateErrorResponse的WebAPI(OWIN)全局异常记录 - WebAPI(OWIN) Global Exception Logging with CreateErrorResponse
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM