私はExceptionsFilterAttribute
を使用しているエラーを管理するためにクライアントのサービスを開発するためにWeb API 2を使用していますが、このレベルではすべての例外がキャッチされません。私はWebApiConfigでGlobalExceptionHandler
Webapi 2のグローバル例外処理が行われていません
public class GlobalExceptionHandler: ExceptionHandler
{
//A basic DTO to return back to the caller with data about the error
private class ErrorInformation
{
public string Message { get; set; }
public DateTime ErrorDate { get; set; }
}
public override void Handle(ExceptionHandlerContext context)
{
//Return a DTO representing what happened
context.Result = new ResponseMessageResult(context.Request.CreateResponse(HttpStatusCode.InternalServerError,
new ErrorInformation { Message="We apologize but an unexpected error occured. Please try again later.", ErrorDate=DateTime.UtcNow }));
//This is commented out, but could also serve the purpose if you wanted to only return some text directly, rather than JSON that the front end will bind to.
//context.Result = new ResponseMessageResult(context.Request.CreateResponse(HttpStatusCode.InternalServerError, "We apologize but an unexpected error occured. Please try again later."));
}
}
を作成 多少の誤差がprotected void Application_AuthenticateRequest(Object sender, EventArgs e)
で育っていると私はそれらを処理し、彼にエラーの詳細を与えるために私たちのクライアントにカスタムメッセージを送って、これを解決するためにしたい私は、この行を追加しました:
config.Services.Replace(typeof(IExceptionHandler), new GlobalExceptionHandler());
Application_AuthenticateRequest
は、いくつかのエラーを上げるが、GlobalExceptionHandler
は到達しません。
この問題を解決するにはどうすればよいですか?
ありがとうございます。
にスローされた例外をキャッチするGlobal.asax.csファイル内のApplication_Errorを使用し、私はIAuthenticationFilterを使用し、私のためにうまく働きました –