I have WebGet, and WebInvoke attributes describing my contract, but what is the best method of handling invalid URI's? Right now, if a user passes an URI that does not match my current operations, they get an "Endpoint not found." message. I want to pass back a more descriptive message.
For example, my URI template looks like:
/Stuff/{ID}/subStuff
but say they type
/Stuff/{ID}/OtherStuff
There is no such thing as OtherStuff, and I do not have a template for that.
Is there a way to cover all non mapped URI's with a single contract?
Thanks!
While I did follow the links mark provided, and they did give a hint of what I needed. The answers that were linked did not actually answer my original question.
I was able to follow the steps, and I wanted to list my steps to solve this problem on this question as well.
To create my own response to any URI that was not mapped to a method in my contract I created the following:
Below are the full definitions of the object's I created:
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.
If you want to catch all the unhandled requests at a global level in WCF REST then you have to create a custom
WebHttpBehavior
and customIOperationInvoker
as described in this post.If you want to return a custom error text with custom status code(404) you can also look into the
WebOperationContext.OutgoingResponse
property as described here.