【发布时间】:2023-03-26 22:50:02
【问题描述】:
我在 IIS 7.5 上有一个 wcf 休息服务。当有人访问不存在的端点的一部分时(即http://localhost/rest.svc/DOESNOTEXIST vs http://localhost/EXISTS),他们会看到一个带有状态代码 404 的通用 WCF 灰色和蓝色错误页面。但是,我想返回类似以下:
<service-response>
<error>The url requested does not exist</error>
</service-response>
我尝试在 IIS 中配置自定义错误,但它们仅在请求 REST 服务之外的页面时才有效(即http://localhost/DOESNOTEXIST)。
有人知道怎么做吗?
编辑 在下面的答案之后,我能够弄清楚我需要创建一个实现 BehaviorExtensionElement 的 WebHttpExceptionBehaviorElement 类。
public class WebHttpExceptionBehaviorElement : BehaviorExtensionElement
{
///
/// Get the type of behavior to attach to the endpoint
///
public override Type BehaviorType
{
get
{
return typeof(WebHttpExceptionBehavior);
}
}
///
/// Create the custom behavior
///
protected override object CreateBehavior()
{
return new WebHttpExceptionBehavior();
}
}
然后我可以通过以下方式在我的 web.config 文件中引用它:
<extensions>
<behaviorExtensions>
<add name="customError" type="Service.WebHttpExceptionBehaviorElement, Service"/>
</behaviorExtensions>
</extensions>
然后添加
<customError />
到我的默认端点行为。
谢谢,
杰弗里·凯文·普瑞
【问题讨论】:
-
您使用的是 REST Starter 工具包还是 WCF Web API?
标签: c# wcf rest exception-handling