I’m building a RESTful API and I would like to control all possible output to my consumers. I’m implementing an ExceptionFilterAttribute to filter all exceptions raised in my controllers. This, however, doesn’t give me control over errors that might happen in my application prior reaching controller code – such as routing errors. Default behaviour sends back a standard serialized HttpError giving away too much internal information to my taste, such as controller classnames etc. I would like to avoid that. What is the best way of changing this behaviour?
I’m building a RESTful API and I would like to control all possible output
Share
You can add a
MessageHandlerto do this.MessageHandlersrun first and last in the pipeline, allowing you to process raw incoming request and raw outgoing response.For example:
And then register in your
GlobalConfigurationThis basically inspects the outgoing response and checks if the status code is 2xx. If not you can do something with it – log, or perhaps reset the content of the response to hide whatever you wanna hide.