I have a Web API project that returns some product data.
If no Accept header is specified it returns XML as default, done like so in my WebApiConfig
:
config.Formatters.Clear();
config.Formatters.Add(new XmlMediaTypeFormatter());
config.Formatters.Add(new JsonMediaTypeFormatter());
config.Formatters.Add(new FormUrlEncodedMediaTypeFormatter());
So default is XML, the first formatter, but the API still supports JSON if the request asks for it.
In my ControllerHelper
, I added a 415 Format not supported response:
catch (FormatException)
{
var resp = new HttpResponseMessage(HttpStatusCode.UnsupportedMediaType)
{
Content = new StringContent(string.Format(HttpStatusCode.UnsupportedMediaType.ToString())),
};
throw new HttpResponseException(resp);
}
And I would like to throw that response if no Accept header is specified and therefore require it to be set, either to application/xml
, text/xml
or application/json
.
For example, if I test to set accept in Advanced Rest Client to application/foo
I want to throw an exception.
How to do this? Thanks in advance!
No comments:
Post a Comment