RicoSuter/NSwag

Web api 2 with api version in url

Open

#1,554 opened on Aug 25, 2018

View on GitHub
 (2 comments) (0 reactions) (0 assignees)C# (1,189 forks)batch import
help wantedtype: question

Repository metrics

Stars
 (6,291 stars)
PR merge metrics
 (No merged PRs in 30d)

Description

Hello,

I'm using these nswag packages in a .net 4.6 application (with web api 2)

packages

My api is url versioned (myserver/api/v1/echo?id=test)

I have this configuration for the versioning :

var constraintResolver = new DefaultInlineConstraintResolver()
{
      ConstraintMap =
      {
          ["apiVersion"] = typeof( ApiVersionRouteConstraint )
      }
};

config.MapHttpAttributeRoutes(constraintResolver, new CentralizedPrefixProvider("api/v{version:apiVersion}"));
config.AddApiVersioning(o => o.ReportApiVersions = true);

And my controllers look like this

   [ApiVersion("1.0")]
    public class EchoController : ApiController
    {
        [Route("echo")]
        [NSwag.Annotations.SwaggerTag("System Check")]
        public HttpResponseMessage Get(string id)
        {
            return Request.CreateResponse<string>(id);
        }
    }

I used this for swagger generation (with the WebApiToSwaggerGenerator)

app.UseSwagger(typeof(Startup).Assembly, settings =>
{

});

app.UseSwaggerUi3(typeof(Startup).Assembly, settings =>
{
			
});

When I generate the swagger file, I don't have the version specified, I only have

  "paths": {  
   "/echo": {
      "get": {

instead of

  "paths": {
   "/api/v1/echo": {
      "get": {

I tried to force different routes without any success

app.UseSwagger(typeof(Startup).Assembly, settings =>
{
	settings.GeneratorSettings.DefaultUrlTemplate = "api/v{version:apiVersion}/{controller}/{id}";
});

Could you help figuring out how to configure the version ?

Contributor guide