Comments (2)
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.
from aspnet-api-versioning.
@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).
from aspnet-api-versioning.
Related Issues (20)
- ASP.NET Web API versioning Migrate from QueryStringApiVersionReader to UrlSegmentApiVersionReader HOT 2
- only the 5.1.0 version of Microsoft.AspNetCore.Mvc.Versioning is deprecated HOT 3
- Problem with describing reponse codes in minimal api HOT 3
- Cannot run APIs with different controller names with same ControllerName attribute after migration HOT 6
- Different options in `ApiVersioningOptions.cs` between .NET Framework and .NET Core packages HOT 2
- WithOpenApi() ignore Api versioning readers HOT 4
- .net 8 support HOT 7
- Breaking changes when migrating to OData8 + new versioning HOT 10
- odata/$metadata returns 404 when all controllers are decorated with ApiVersionNeutralAttribute HOT 3
- AddVersionedApiExplorer not working in Asp.Versioning HOT 5
- VersionedApiDescriptionProvider does not set the correct SunsetPolicy to ApiDescription instances HOT 1
- Using ApiExplorerSettingsAttribute together with ApiVersionAttribute produces unexpected number of ApiVersionDescriptions HOT 5
- Asp.Net Core WebApi - AWS ECS Cluster Authentication failure HOT 2
- [Versioned Clients][API Notifications] Fails to read new versions when available HOT 2
- Swashbuckle documentation inconsistent with examples HOT 2
- AssumeDefaultVersionWhenUnspecified does not work correctly if ApiVersionNeutral is used in the controller HOT 2
- swagger.json file not found after update HOT 9
- My API is not displaying all the versions. HOT 4
- Improve docs for HeaderApiVersionReader
- Add synonym to `AddMvc` method. HOT 7
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from aspnet-api-versioning.