Skip to content

Wrong route variable replacing + language questions #419

Closed
@LbISS

Description

@LbISS

Hi, guys.

I'm moving from SDammann.Versioning lib to this project and the process of configuration is better and running more smoothly. Thanks contributors for a great work. As for API itself all is working great, but i have two questions while trying configure the swagger/swashbuckle.

I have removed all conventional routing and left only attribute based one. Currently my route prefix for controllers looks like {language:regex(ru|en|es)}/api/v{version:apiVersion}
I'm configuring my app with the following code:

public class Config
{
	/// <summary>
	/// The route prefix
	/// </summary>
	public const string RoutePrefix = @"{language:regex(ru|en|es)}/api/v{version:apiVersion}/";
	
	/// <summary>
	/// Registers API settings in the specified configuration.
	/// </summary>
	/// <param name="config">The configuration.</param>
	/// <param name="container">The container.</param>
	public static void Register(HttpConfiguration config, IContainer container)
	{
		...
	
		var constraintResolver = new DefaultInlineConstraintResolver()
		{
			ConstraintMap =
			{
				["apiVersion"] = typeof( ApiVersionRouteConstraint )
			}
		};
	
		// Attribute routing.
		config.MapHttpAttributeRoutes(constraintResolver);
	
		config.AddApiVersioning();
	
		//Swagger
		var apiExplorer = config.AddVersionedApiExplorer(
				options =>
				{
					options.GroupNameFormat = "'v'VVV";
					options.SubstituteApiVersionInUrl = true;
				});

		config
			.EnableSwagger("{apiVersion}/swagger", 
				c =>
				{
					c.MultipleApiVersions(
						(apiDescription, version) => apiDescription.GetGroupName() == version,
						info =>
						{
							foreach (var group in apiExplorer.ApiDescriptions)
							{
								var description = "";
								if (group.IsDeprecated)
								{
									description += "This API version has been deprecated.";
								}

								info.Version(group.Name, $"Restaurants API {group.ApiVersion}")
									.Description(description);
							}
						});
	
					...
	
					c.OperationFilter<SwaggerAuthOperationFilter>(); 
					c.OperationFilter<SwaggerDefaultFilter>();

					...

					c.ResolveConflictingActions(apiDescriptions => apiDescriptions.First());
				})
			.EnableSwaggerUi(c =>
				{
					c.EnableDiscoveryUrlSelector();
				});
	}
}

And i have the next controller:

[RoutePrefix(Config.RoutePrefix + "test")]
[ApiVersion("1.0")]
[ApiVersion("1.1")]
[ApiVersion("2.0")]
...
public class TestController : BaseController
{
	/// <summary>
	/// Used to check if server alive.
	/// </summary>
	/// <returns>Time and version of server</returns>
	[HttpGet]
	[Route("")]
	[ResponseType(typeof(TestResult))]
	public IHttpActionResult Get()
	{
		
		return Ok(new TestResult
		{
			Result = @"Success",
			ServerDateTime = DateTimeOffset.Now,
			Build = GetExecutingAssemblyModifiedDate().ToString("yyyy.MM.dd HH:mm")
		});
	}
}

After running app and opening swagger i'm seeing the next route:
s1

  1. Why {language} tag have been replaced with version instead of {version} tag? How can i prevent this behaviour?
  2. If i'll hardcode "en" for {language} param:
    RoutePrefix = "en/api/v{version:apiVersion}/"
    then the version tag is substituting for v1. But request from swagger not working nevertheless - it's going on "en/api/v1/test", but the application returns 406.0 - Not Acceptable, it's awaiting request on "en/api/v1.0/test". How can setup swagger correctly?

I have ASP.Net WebAPI 2 project (.net 4.7.2.) with Swashbuckle 5.6.0 & Microsoft.AspNet.WebApi.Versioning.ApiExplorer 3.0.0

I've found this issue: #113, but for now i can't use approach with headers due to neccessity of backward compatibility.

I'll appreciate any help.

Metadata

Metadata

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions