【发布时间】:2016-10-24 06:34:15
【问题描述】:
我正在使用 Swashbuckle(一个 .NET 包装器)。
当我生成我的 swagger 文档时,它会根据控制器名称/参数生成文档中的 URI,而不是在应用程序启动时在配置中定义的已定义路由。
所以我有一个控制器:
namespace Pat.PostingService.Api.Version1
{
public class DeliveryStatusController : ApiController
{
[ResponseType(typeof(DeliveryStatusDto))]
public dynamic Get(string deliveryType, Guid? orderId = null)
{
...
}
}
{
在 RouteConfig.cs 文件中,我重新定义了路由:
public void Configuration(IAppBuilder appBuilder)
{
var config = new HttpConfiguration();
...
config.Routes.MapHttpRoute(
name: "DeliveryStatusService",
routeTemplate: "SpecialDeliveryServiceStatus/{deliveryType}/{orderId}",
defaults: new {
controller = "DeliveryStatus",
orderId = RouteParameter.Optional
}
);
config.Routes.MapHttpRoute(
name: "Default",
routeTemplate: "{controller}/{id}",
defaults: new { id = RouteParameter.Optional }
);
}
调用 API 时,我现在无法使用 /DeliveryStatus 端点,我必须使用 /SpecialDeliveryServiceStatus 端点。
但是,Swagger 文档指出端点是 /DeliveryStatus。
与我的同事一起,我们相信 _apiExplorer.ApiDescriptions(来自 SwaggerGenerator.cs 在 Swashbuckle.Core 中使用的 System.Web.Http.Description.IApiExplorer)正在向我们返回不正确的路由,尽管我们可能是错误的。
我们错过了什么?我们可以使用什么来确保 Swagger 使用配置中使用的路由而不是默认路由?
编辑:
我们还使用 SDamann 进行版本控制(想象在 version2 文件夹中的第二个 RouteConfig.cs 文件),它不支持 [AttributeRouting],因此我们需要从启动路由配置中获取路由。
编辑 2:
将action = "Get" 设置为路由的默认值也不能解决问题。
【问题讨论】:
-
这很奇怪,我刚刚测试了你的代码,我得到了一个完美的结果......它向我显示了端点为 /SpecialDeliveryServiceStatus/{deliveryType}/{orderId}。尝试创建一个 ISchemaFilter 并检查 ApiDescription - 如果您愿意,您可以在此处更改路径,尽管这将需要大量静态代码和大量 operationId(如果这是您唯一的端点,则不会有问题)
标签: c# asp.net swagger swagger-ui swashbuckle