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
I had a similar problem, and the other answer did lead to my eventual success, it was not the clearest of answers. Below is the way I solved this issue.
My projects setup is a WCF service hosted as a svc hosted in IIS. I could not go with the configuration route for adding the behavior, because my assembly versions change every checkin due to continuous integration.
to overcome this obsticle, I created a custom ServiceHostFactory:
As you can see above, we are adding a new behavior: WcfUnknownUriBehavior. This new custom behavior's soul duty is to replace the UnknownDispatcher. below is that implementation:
Once you have these objects specified, you can now use the new factory within your svc's "markup":
And that should be it. as long as your object "YourResponseObject" can be serialized, it's serialized representation will be sent back to the client.
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.