0

我有一个 REST 服务,我想有一个帮助类来处理异常

我的代码如下所示:

[WebGet(UriTemplate = "/Test/{param}")]
public Stream Test(string param)
{
        if (param == "Ok")
            return Process(param);
        else
        {
            RESTException(System.Net.HttpStatusCode.BadRequest, "Param not ok");
            return null;
        }
}


private void RESTException(System.Net.HttpStatusCode httpStatusCode, string message)
{
        OutgoingWebResponseContext response = WebOperationContext.Current.OutgoingResponse;
        response.StatusCode = httpStatusCode; // or anything you want
        response.StatusDescription = message;
}

我从浏览器测试,但是当我传递错误的参数时,例如

http://myaddress/Service/Test/badparam

浏览器中没有任何显示。

我的代码有什么问题?

4

2 回答 2

1

改变

RESTException(System.Net.HttpStatusCode.BadRequest, "Param not ok");
return null;

RESTException(System.Net.HttpStatusCode.BadRequest, "Param not ok");
return "Param not ok";
于 2012-02-04T11:22:39.950 回答
0

我将 RESTException 更改如下,当从浏览器或 REST WCF WebApi 提供的 REST 测试客户端测试 REST 时,它会引发正确的异常,并且 slsdo 会显示正确的异常信息

    private void RESTException(SelectFault fault, System.Net.HttpStatusCode httpStatusCode)
    {
        throw new WebFaultException<string>(fault.ErrorMessage, httpStatusCode);
    }
于 2012-04-03T13:57:12.123 回答