Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Deprecate specific route out of multiple routes on single Web API method

Hi I have WEB API implementation as shown below. Where we are using multiple routes on single method.

[SwaggerOperation("Update Records By Id")]
    [Route("/construction/field-record")]
    [Route("/construction/fieldRecord")]
    public async Task<IActionResult> UpdateRecord([FromBody] UpdateRecordRequest request)

Two questions,

  1. How to mark only one route out of two as deprecated?
  2. How to update swagger indicating that route is "Deprecated" on sawagger UI?

-Thanks

like image 647
user3249448 Avatar asked Feb 02 '18 19:02

user3249448


People also ask

Can we have multiple get methods in Web API?

As mentioned, Web API controller can include multiple Get methods with different parameters and types. Let's add following action methods in StudentController to demonstrate how Web API handles multiple HTTP GET requests.

What is App UseRouting ()?

UseRouting adds route matching to the middleware pipeline. This middleware looks at the set of endpoints defined in the app, and selects the best match based on the request. UseEndpoints adds endpoint execution to the middleware pipeline. It runs the delegate associated with the selected endpoint.

Which method is generally used for registering Web API routes?

Attribute routing is supported in Web API 2. As the name implies, attribute routing uses [Route()] attribute to define routes. The Route attribute can be applied on any controller or action method. In order to use attribute routing with Web API, it must be enabled in WebApiConfig by calling config.


1 Answers

You can add an OperationFilter that checks the RelativePath. This string contains the designation of the route. Just specify the HttpPost/HttpGet-routes

[HttpPost("/construction/field-record")]
[HttpPost("/construction/fieldRecord")]

and then use the following operation filter

public class ExplicitObsoleteRoutes : IOperationFilter
{
    public void Apply(OpenApiOperation operation, OperationFilterContext context)
    {
        if (context.ApiDescription.RelativePath.EndsWith("fieldRecord"))
        {
            operation.Deprecated = true;
        }
    }
}
like image 64
Matthias Müller Avatar answered Oct 19 '22 23:10

Matthias Müller