我正在使用web api 2为客户端开发服务,管理我们使用ExceptionsFilterAttribute错误,但是如你所知,在这个级别中并不是所有异常都被捕获。 在protected void Application_AuthenticateRequest(Object sender, EventArgs e)中引发了一些错误,我想处理它们并向我们的客户端发送自定义消息,以便给出更多有关错误的详细信息,为此我创建一个GlobalExceptionHandler
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.")); } }在WebApiConfig中我添加了这一行:
config.Services.Replace(typeof(IExceptionHandler), new GlobalExceptionHandler());Application_AuthenticateRequest引发了一些错误,但从未到达GlobalExceptionHandler 。
你知道我怎么能解决这个问题?
提前致谢。
I'm uising web api 2 to develop services for a client, to manage errors we are using a ExceptionsFilterAttribute, but as you know, in this level not all exception are catched. Some errors are raised in protected void Application_AuthenticateRequest(Object sender, EventArgs e) and I want to handle them and send a custom message to our client to give him more details about the error, to solve this I create a GlobalExceptionHandler
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.")); } }In WebApiConfig i added this line :
config.Services.Replace(typeof(IExceptionHandler), new GlobalExceptionHandler());The the Application_AuthenticateRequest raise some errors but GlobalExceptionHandler is never reached.
Do you have any idea how can I solve this issue ?
Thanks in advance.
最满意答案
Application_AuthenticateRequest不在Web API管道中。 因此,如果在此方法中抛出异常,则可以通过Web API异常处理程序捕获这些异常,因为在Web API管道启动之前会抛出异常。
有两种方法可以做到这一点:
更改身份验证机制并使用Web API身份验证(IAuthenticationFilter)而不是Application_AuthenticateRequest。
如果这个项目只有与Web API相关的控制器,而不是像MVC那样。或者在Global.asax.cs文件中使用Application_Error来捕获Application_AuthenticateRequest中引发的异常
Application_AuthenticateRequest does not come in the Web API pipeline. So if an exception is thrown in this method those can be caught by the Web API exception handler, because the exception is thrown before the Web API pipeline is started.
There are two ways to do this:
Either change the authentication mechanism and use Web API Authentication(IAuthenticationFilter) instead of Application_AuthenticateRequest.
If this project has only Web API related controllers, not like MVC and all.Or use Application_Error in the Global.asax.cs file to catch the exception thrown in Application_AuthenticateRequest
更多推荐
发布评论