我有一个RequestDto,它接受 int 参数
[Route("/club/thread/{Id}","GET")]
public MyDto{
[Apimember(DataType="int32")]
public int Id{get;set;}
}
当我输入http://myservice/club/thread/aaa.json
时,抛出异常:
[RequestBindingException:无法绑定请求]
ServiceStack.WebHost.Endpoints.RestHandler.GetRequest(IHttpRequest httpReq,IRestPath restPath)+538
ServiceStack.WebHost.Endpoints.RestHandler.ProcessRequest(IHttpRequest httpReq,IHttpResponse httpRes,String operationName)+1023
System.Web.CallHandlerExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute()+913
System.Web.HttpApplication.ExecuteStep(IExecutionStep step,Boolean& completedSynchronously)+165
AppHost.ExceptionHandler无法捕获此异常,我该如何捕获它?
答案 0 :(得分:4)
请求绑定异常是在服务请求的上下文之前抛出的未捕获异常,因此由UncaughtExceptionHandlers
处理,可以在AppHost中注册:
public override void Configure(Container container)
{
//Custom global uncaught exception handling strategy
this.UncaughtExceptionHandlers.Add((req, res, operationName, ex) =>
{
res.StatusCode = 400;
res.Write(string.Format("Exception {0}", ex.GetType().Name));
res.EndRequest(skipHeaders: true);
});
}
或者覆盖AppHost的OnUncaughtException
方法,例如:
public override void OnUncaughtException(
IRequest httpReq, IResponse httpRes, string operationName, Exception ex)
{
"In OnUncaughtException...".Print();
base.OnUncaughtException(httpReq, httpRes, operationName, ex);
}