0

I want to call service with the same routing name(same parameter ) with different versions ...

bellow is the my code

[Route("api/v{version:apiVersion}/[controller]/")]
    [ApiController]
    [ApiVersion("1.0")]
    [ApiVersion("1.1")]
    public class AccountController : ControllerBase
    {
    [MapToApiVersion("1")]
    [HttpGet("getacounttypes")]
    [ProducesResponseType(typeof(Models.ReturnString), StatusCodes.Status200OK)]
    [ProducesResponseType(typeof(Models.ErrorMessage), StatusCodes.Status400BadRequest)]
    [ProducesResponseType(typeof(Models.ErrorMessage), StatusCodes.Status500InternalServerError)]
    public async Task<ActionResult> GetAddressTypes()
    {
        logger.LogInformation("Request is processing at account types");
        try
        {
            return Ok(await accountBS.GetAccountTypes());
        }
        catch (ArgumentException ex)
        {
            logger.LogError(ex, ex.Message);
            this.Response.StatusCode = StatusCodes.Status400BadRequest;
            return new JsonResult(new Models.ErrorMessage() { Code = StatusCodes.Status400BadRequest.ToString(), Message = ex.Message });
        }
        catch (Exception e)
        {
            logger.LogError(e, e.Message);
            this.Response.StatusCode = StatusCodes.Status500InternalServerError;
            return new JsonResult(new Models.ErrorMessage() { Code = StatusCodes.Status500InternalServerError.ToString(), Message = e.Message });
        }
    }


    [MapToApiVersion("1.1")]
    [HttpGet("getacounttypes")]
    [ProducesResponseType(typeof(Models.ReturnString), StatusCodes.Status200OK)]
    [ProducesResponseType(typeof(Models.ErrorMessage), StatusCodes.Status400BadRequest)]
    [ProducesResponseType(typeof(Models.ErrorMessage), StatusCodes.Status500InternalServerError)]
    public async Task<ActionResult> GetAddressTypesV1_1()
    {
        logger.LogInformation("Request is processing at account types");
        try
        {
            return Ok(await accountBS.GetAccountTypes());
        }
        catch (ArgumentException ex)
        {
            logger.LogError(ex, ex.Message);
            this.Response.StatusCode = StatusCodes.Status400BadRequest;
            return new JsonResult(new Models.ErrorMessage() { Code = StatusCodes.Status400BadRequest.ToString(), Message = ex.Message });
        }
        catch (Exception e)
        {
            logger.LogError(e, e.Message);
            this.Response.StatusCode = StatusCodes.Status500InternalServerError;
            return new JsonResult(new Models.ErrorMessage() { Code = StatusCodes.Status500InternalServerError.ToString(), Message = e.Message });
        }
    }
}

in this im having error like

An unhandled exception has occurred while executing the request.
System.NotSupportedException: HTTP method "GET" & path "api/v{version}/Account/[actions]/getacounttypes" overloaded by actions - API.Controllers.AccountController.GetAddressTypes (GSOnline.API),
API.Controllers.AccountController.GetAddressTypesV1_1 . Actions require unique method/path combination for Swagger 2.0. Use ConflictingActionsResolver as a workaround
   at Swashbuckle.AspNetCore.SwaggerGen.SwaggerGenerator.CreatePathItem(IEnumerable`1 apiDescriptions, ISchemaRegistry schemaRegistry)
   at System.Linq.Enumerable.ToDictionary[TSource,TKey,TElement](IEnumerable`1 source, Func`2 keySelector, Func`2 elementSelector, IEqualityComparer`1 comparer)
   at Swashbuckle.AspNetCore.SwaggerGen.SwaggerGenerator.CreatePathItems(IEnumerable`1 apiDescriptions, ISchemaRegistry schemaRegistry)
   at Swashbuckle.AspNetCore.SwaggerGen.SwaggerGenerator.GetSwagger(String documentName, String host, String basePath, String[] schemes)
   at Swashbuckle.AspNetCore.Swagger.SwaggerMiddleware.Invoke(HttpContext httpContext, ISwaggerProvider swaggerProvider)
   at Microsoft.AspNetCore.Builder.RouterMiddleware.Invoke(HttpContext httpContext)
   at Microsoft.AspNetCore.Builder.RouterMiddleware.Invoke(HttpContext httpContext)
   at Microsoft.AspNetCore.StaticFiles.StaticFileMiddleware.Invoke(HttpContext context)
   at Microsoft.AspNetCore.Diagnostics.StatusCodePagesMiddleware.Invoke(HttpContext context)
   at Microsoft.AspNetCore.Diagnostics.EntityFrameworkCore.MigrationsEndPointMiddleware.Invoke(HttpContext context)
   at Microsoft.AspNetCore.Diagnostics.EntityFrameworkCore.DatabaseErrorPageMiddleware.Invoke(HttpContext httpContext)
   at Microsoft.AspNetCore.Diagnostics.EntityFrameworkCore.DatabaseErrorPageMiddleware.Invoke(HttpContext httpContext)
   at Microsoft.AspNetCore.Diagnostics.DeveloperExceptionPageMiddleware.Invoke(HttpContext context)

so could u please suggest me the solution for the same routing with same parameters with diff versions ?? i can manage method with diff parameters but im looking same routing with same parametrs ...

expecting result is

GET
/api/v1/Account/getacounttypes
GET
/api/v1.1/Account/getacounttypes
user2564537
  • 1,041
  • 1
  • 12
  • 16

1 Answers1

0

Your question is not entirely clear, but it appears you are referring to generating a single OpenAPI/Swagger document with Swashbuckle. Normally, Swashbuckle will create a single document per API version. Using the URL segment method, it is possible to create a single document with all of the URLs. The document only allows distinct URLs, which are derived from route templates. The route parameter is filled in at request time, but the document generator doesn't know or otherwise understand that.

To achieve this behavior, you need to have the API Explorer fill in the API version when it generates descriptions. Your configuration should look like this:

services.AddVersionedApiExplorer(options => options.SubstituteApiVersionInUrl = true);

With this option configured, the API version will be substituted automatically on your behalf and achieve your expected results.

Dharman
  • 30,962
  • 25
  • 85
  • 135
Chris Martinez
  • 3,185
  • 12
  • 28