I'm learning Web Api and have bumped into the following issue. Similar to this question.
My exceptions are still returning as XML in Chrome, but JSON in IE. The exceptions are thrown as XML when inheriting from the ODataController but are correctly thrown as JSON when inheriting from ApiController.
My WebApiConfig class:
public static void Register(HttpConfiguration config)
{
// Web API configuration and services
config.Filters.Add(new NotImplementedExceptionFilterAttribute());
config.Filters.Add(new MethodAttributeExceptionHandling());
config.Services.Replace(typeof(IHttpActionInvoker), new CustomApiControllerActionInvoker());
// Web API routes
ODataConventionModelBuilder builder = new ODataConventionModelBuilder();
builder.EntitySet<EventType>("EventTypes");
config.Routes.MapODataRoute("ODataRoutes", "api", builder.GetEdmModel());
//Route Configuration
config.Routes.MapHttpRoute(
name: "DefaultApi",
routeTemplate: "api/{controller}/{action}"
);
var appXmlType = config.Formatters.XmlFormatter.SupportedMediaTypes.FirstOrDefault(t => t.MediaType == "application/xml");
config.Formatters.XmlFormatter.SupportedMediaTypes.Remove(appXmlType);
}
I have a CustomApiControllerActionInvoker verbatim from: http://www.codeproject.com/Articles/733512/Exception-Handling-in-WebAPI
My TestController inheriting from ApiController:
public HttpResponseMessage GetGlobalErrorMessage()
{
int i = 0;
var val = 10 / i;
return new
HttpResponseMessage(HttpStatusCode.OK);
}
returns JSON in Chrome:
{
"Message": "Oops some internal Exception. Please contact your administrator",
"ErrorCode": 500
}
However, if I inherit from ODataController with similar code:
[Queryable]
public IQueryable<EventType> Get()
{
int i = 0;
var val = 10 / i;
return _repo.EventTypes();
}
I'm returned XML from the exception in Chrome:
<m:error xmlns:m="http://schemas.microsoft.com/ado/2007/08/dataservices/metadata">
<m:code/>
<m:message xml:lang="en-US">
Oops some internal Exception. Please contact your administrator
</m:message>
</m:error>
Summary:
How do I force consistent content types for payloads as well as exceptions, still allowing flexibility for native content negotiation using OData Web Api?