I have a wcf rest service on IIS 7.5. When someone visits a part of the endpoint that doesn’t exist (i.e. http://localhost/rest.svc/DOESNOTEXIST vs http://localhost/EXISTS) they are presented with a Generic WCF gray and blue error page with status code 404. However, I would like to return something like the following:
<service-response>
<error>The url requested does not exist</error>
</service-response>
I tried configuring the custom errors in IIS, but they only work if requesting a page outside of the rest service (i.e. http://localhost/DOESNOTEXIST).
Does anyone know how to do this?
Edit
After the answer below I was able to figure out I needed to create a WebHttpExceptionBehaviorElement class that implements BehaviorExtensionElement.
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();
}
}
I was then able to reference it in my web.config file via:
<extensions>
<behaviorExtensions>
<add name="customError" type="Service.WebHttpExceptionBehaviorElement, Service"/>
</behaviorExtensions>
</extensions>
And then adding
<customError />
to my default endpoint behaviors.
Thanks,
Jeffrey Kevin Pry
First, create a custom behavior which subclasses WebHttpBehavior – here you will remove the default Unhandled Dispatch Operation handler, and attach your own:
Then. make your unknown operation handler. This class will handle the unknown operation request and generate a “Message” which is the response. I’ve shown how to create a plain text message. Modifying it for your purposes should be fairly straight-forward:
At this point you have to associate the new behavior with your service.
There are several ways to do that, so just ask if you don’t already know, and i’ll happily elaborate further.