Git Product home page Git Product logo

Comments (2)

commonsensesoftware avatar commonsensesoftware commented on May 25, 2024 1

I was hestitant to even include this capability at all, but ultimately:

  1. There was a benefit to having a factory for creating error responses. I chose a delegate over an interface.
  2. 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:

  1. Subclass BadRequestObjectResult, change the StatusCode property to 200 (OK), and then return your custom object result in the ApiVersioningOptions.CreateBadRequest delegate.
  2. Intercept the default ApiVersioningOptions.CreateBadRequest delegate and just change the StatusCode of the original result.
  3. 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.

BillDines avatar BillDines commented on May 25, 2024

@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)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.