我有一个运行在IIS 7.5上的WCF REST服务。当有人访问一个不存在的端点时(例如:http://localhost/rest.svc/DOESNOTEXIST与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 />
关于我的默认端点行为的问题。
谢谢,
Jeffrey Kevin Pry