2014-03-19 47 views
1

我有一個RequestDto,它接受一個INT參數如何捕捉ServiceStack RequestBindingException

[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,字符串operationName)1023

System.Web.CallHandlerExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute()913

System.Web.HttpApplication.ExecuteStep(IExecutionStep一步,布爾& completedSynchronously)+ 165

AppHost.ExceptionHandler不能趕上這個例外,我怎樣才能抓住它?

回答

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); 
}