error-handling - ServiceStack-如何从RequestFilter返回ResponseDTO?

标签 error-handling request servicestack

我正在使用RequestFilter预处理一些发送到Web服务的消息,如果有错误,我想返回ResponseDTO,然后终止对该请求的进一步处理。如何实现该功能?

另外,我通过在RequestDTO类上使用[RequestFilter]装饰器来实现此目的。如果我想拥有多个请求过滤器,如何选择用于给定RequestDTO的过滤器?

最佳答案

如果有错误,我想返回ResponseDTO,然后终止对该请求的进一步处理。如何实现该功能? 不太清楚如何确定错误,因此这是一个非常基本的解决方案。

public class RequestFilterAttribute : Attribute, IHasRequestFilter
{
    public void RequestFilter(IHttpRequest req, IHttpResponse res, object requestDto)
    {
        if(requestDto.GetType() == typeof(YourRequestType))
        {
            //code to check for errors - if error throw exception 
            throw new Exception("Exception for your request type");
        }
    }
}

如果我想要多个请求过滤器,如何选择用于给定RequestDTO的过滤器?

您可以创建几个不同的RequestFilter实现,并使用不同的实现来装饰每个DTO。
public class FilterOneAttribute : Attribute, IHasRequestFilter
{
    //Code
}

public class FilterTwoAttribute : Attribute, IHasRequestFilter
{
    //Code
}

[FilterOne]
public class OneClass 
{
    //Code
}

[FilterTwo]
public class TwoClass
{
    //Code
}

关于error-handling - ServiceStack-如何从RequestFilter返回ResponseDTO?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15141469/

相关文章:

asp.net-mvc - 错误处理捕获 View 渲染异常的最后机会

C++ ifstream 错误检查

mobile - 如何在Web服务器上检测移动客户端?

.net - ServiceStack认证失败时,不重定向?

c++ - 带有 std::error_code 的错误堆栈

php - PHP处理脚本包含而不抑制其错误和警告

c# - 如何从 http 请求/响应流中获取 PDF

Java servlet。登录后如何访问之前的网址

c# - 仅在找到/存在实例时才使用 Redis 进行缓存?

razor - ServiceStack 在 razor 页面中包含另一个 razor 页面