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,
-Thanks
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.
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.
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.
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;
}
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With