RicoSuter/NSwag

Web api 2 with api version in url

Open

#1,554 创建于 2018年8月25日

在 GitHub 查看
 (2 评论) (0 反应) (0 负责人)C# (1,189 fork)batch import
help wantedtype: question

仓库指标

Star
 (6,291 star)
PR 合并指标
 (30 天内没有已合并 PR)

描述

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 ?

贡献者指南