使用服务器端异步时,Se​​rviceStack Forbidden(403)错误作为内部服务器错误(500)返回

时间:2014-01-30 01:57:33

标签: jquery http servicestack

我有删除实体的简单请求。在实现中,当无法删除时,我会抛出具有403(禁止)状态的HttpError。如果我使服务器实现异步,使用JQuery我得到HTTP 500(内部服务器响应)而不是我在同步实现中获得的预期403响应。

public object Delete(DeleteEntity request)
{
    ...
    throw new HttpError(System.Net.HttpStatusCode.Forbidden, "Cannot delete entity because...");
}

通过这种同步实现,我得到以下响应:

HTTP/1.1 403 Forbidden
Cache-Control: private
Content-Type: text/plain; charset=utf-8
Server: Microsoft-IIS/8.0
X-Powered-By: ServiceStack/4.07 Win32NT/.NET
X-Powered-By: ServiceStack/4.07 Win32NT/.NET
X-AspNet-Version: 4.0.30319
X-SourceFiles: =?UTF-8?B?RDpcU2t5RHJpdmVcRG9jdW1lbnRzXFRlY2hNRFxDb2RlXEFwcGxpY2F0aW9uc1xDQUNcQ0FDLldlYkFwcGxpY2F0aW9uXGFwaVxBcmVhc1wx?=
X-Powered-By: ASP.NET
Access-Control-Allow-Origin: *
Date: Mon, 03 Feb 2014 10:28:14 GMT
Content-Length: 283

相同的服务方法但异步:

public async Task<object> Delete(DeleteEntity request)
{
    ...
    throw new HttpError(System.Net.HttpStatusCode.Forbidden, "Cannot delete entity because...");
}

会产生不同的回复:

HTTP/1.1 500 Internal Server Error
Cache-Control: private
Content-Type: text/html; charset=utf-8
Server: Microsoft-IIS/8.0
X-AspNet-Version: 4.0.30319
X-SourceFiles: =?UTF-8?B?RDpcU2t5RHJpdmVcRG9jdW1lbnRzXFRlY2hNRFxDb2RlXEFwcGxpY2F0aW9uc1xDQUNcQ0FDLldlYkFwcGxpY2F0aW9uXGFwaVxBcmVhc1wx?=
X-Powered-By: ASP.NET
Access-Control-Allow-Origin: *
Date: Mon, 03 Feb 2014 10:16:12 GMT
Content-Length: 5978

这是设计还是错误?另外作为一个附带问题,有没有办法使用JQuery检索异常消息(无需解析jqxhr.responseText字符串并从html文档中提取标题)?

1 个答案:

答案 0 :(得分:1)

这是ASP.NET / IIS中的错误处理问题,请参阅:

您可以通过注册预请求过滤器在ServiceStack中添加建议的解决方法,例如:

this.PreRequestFilters.Add((httpReq, httpRes) => {
    var aspRes = httpRes.OriginalResponse as System.Web.HttpResponseBase;
    if (aspRes != null) 
        aspRes.TrySkipIisCustomErrors = true;
});