Provide the ability to return status codes other than 400 when responding to a request for an unsupported version
I have recently added this package to my Asp.Net core Api and am very impressed with the functionality and ease of use so far.
One small thing that I would like to be able to do would be to return something other than BadRequestObjectResult when a request asks for an unsupported version for a particular resource. The reason for this is that by convention we always return HTTP status 200 if the request is handled in our api. Details of success or failure and any other results are returned in the message body. With this approach it is easy to identify whether responses have come from our Api code or from a web server or other infrastructure.
Currently we can set the ApiVersioningOptions.CreateBadRequest delegate so we can send our normal response, however this can only be a BadRequestObjectResult, which will generate a response with HTTP status code 400. Ideally we would like to be able to use an ObjectResult, so the response has a status code 200.
Looking at the code, this looks like a simple case of changing the return type of CreateBadRequestDelegate to ObjectResult (as well as some renaming so it still makes sense with other return types!). This should be a backwards compatible change as ObjectResult is the base class of BadRequestObjectResult??
thanks
Bill
I was hestitant to even include this capability at all, but ultimately:
- There was a benefit to having a factory for creating error responses. I chose a delegate over an interface.
- Service authors might want to change the error payload.
It was never anticipated that a service author would return anything other than HTTP status code 400 (Bad Request) for an unsupported API version. I would say that returning success for a failed request with a message body that must be interpretted as an error is a violation of the REST constraints. That is the type of service communication we often observed with services that used SOAP over HTTP. Nevertheless, if you'd like to return HTTP status code 200 (OK) for an unsupported API version, it is possible without any design changes.
There isn't anything special about the BadRequestObjectResult. Selecting it as the return type was meant to drive developers toward the proverbial Pit of Success. There are at least 3 simple ways I can think of to meet your requirement:
- Subclass BadRequestObjectResult, change the StatusCode property to 200 (OK), and then return your custom object result in the ApiVersioningOptions.CreateBadRequest delegate.
- Intercept the default ApiVersioningOptions.CreateBadRequest delegate and just change the StatusCode of the original result.
- Define a custom method for ApiVersioningOptions.CreateBadRequest that uses whatever content you want and changes the StatusCode to 200 (OK) before returning the result.
The following is an example of option 2, which is probably the easiest to implement:
using Microsoft.AspNetCore.Http;
services.AddApiVersioning( options =>
{
var newBadRequest = options.CreateBadRequest;
options.CreateBadRequest = ( request, code, message, messageDetail ) =>
{
var result = newBadRequest( request, code, message, messageDetail );
result.StatusCode = StatusCodes.Status200OK;
return result;
};
} );
There are 3 scenarios and error codes where this method will be called:
| Code | Description |
|---|---|
| UnsupportedApiVersion | One or more candidate services were found, but none match the requested API version |
| InvalidApiVersion | An API version was requested, but is malformed (ex: 2016-02-30) |
| AmbiguousApiVersion | Multiple, but different API versions were requested (ex: svc?api-version=1.0&api-version=2.0) |
You may find this information useful in deciding how you craft your custom responses.
@commonsensesoftware - many thanks for the swift and detailed response. Option 2 will work fine for our purposes. I understand your arguments about how RESTful our approach is. This is still under review (and in this case the choice between 400 and 200 is not clear cut). However, our aim is to offer the simplest solution for our likely api consumers (many of whom will not be web developers with a familiarity of REST and HTTP).