c# - Swashbuckle/Swagger 没有从配置中获取路由

标签 c# asp.net swagger swagger-ui swashbuckle

我正在使用 Swashbuckle(一个围绕 swagger 的 .NET 包装器)。

当我生成 swagger 文档时,它会根据 Controller 名称/参数生成文档中的 URI,而不是在应用程序启动时在配置中定义的已定义路由。

所以我有一个 Controller :

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(来自 Swashbuckle.Core 中 SwaggerGenerator.cs 使用的 System.Web.Http.Description.IApiExplorer)返回给我们错误的路线,尽管我们可能错了。

我们错过了什么?我们可以使用什么来确保 Swagger 使用配置中使用的路由而不是默认路由?


编辑:

我们还使用 SDammann 进行版本控制(想象 version2 文件夹中的第二个 RouteConfig.cs 文件),它不支持 [AttributeRouting],因此我们需要从启动路由配置中获取路由。


编辑 2:

action = "Get" 放入路由的默认值中也不能解决问题。

最佳答案

当第一场比赛获胜时,您注册/映射路线的顺序在路线表中起着重要作用。您的示例显示了您如何注册有问题的路线,但没有显示它相对于其他路线的注册位置。

例如,如果您在相关路由之前注册默认路由

public void Configuration(IAppBuilder appBuilder)
{
    var config = new HttpConfiguration();

    ...

    config.Routes.MapHttpRoute(
         name: "Default",
         routeTemplate: "{controller}/{id}",
         defaults: new { id = RouteParameter.Optional }
    );

    config.Routes.MapHttpRoute(
         name: "DeliveryStatusService",
         routeTemplate: "SpecialDeliveryServiceStatus/{deliveryType}/{orderId}",
         defaults: new {
             controller = "DeliveryStatus",
             orderId = RouteParameter.Optional
         }
    );
}

...然后 GET/DeliveryStatus 将按照约定匹配

public dynamic Get(string deliveryType, Guid? orderId = null) { ... }

特别是如果 id 占位符是可选的。

因此请检查以确保您的路由映射顺序正确。默认路由通常最后映射为回退路由。

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 }
    );
}

如果你不反对使用attribute routing in web api 2 ,你可以改为...

namespace Pat.PostingService.Api.Version1
{
    [RoutePrefix("SpecialDeliveryServiceStatus")]
    public class DeliveryStatusController : ApiController
    {
         //GET SpecialDeliveryServiceStatus/{deliveryType}/{orderId}
         [HttpGet]
         [Route("{deliveryType}/{orderId?}")]
         [ResponseType(typeof(DeliveryStatusDto))]
         public dynamic Get(string deliveryType, Guid? orderId = null) { ... }
    }
}

并在RouteConfig.cs文件中,配置属性路由:

public void Configuration(IAppBuilder appBuilder)
{
    var config = new HttpConfiguration();

    config.MapHttpAttributeRoutes();
    ...

}

映射的顺序很重要,这就是为什么它通常在其他映射之前配置。

更新:

尝试以下操作:

config.Routes.MapHttpRoute(
     name: "DeliveryStatusService",
     routeTemplate: "SpecialDeliveryServiceStatus/{deliveryType}/{orderId}",
     defaults: new {
         controller = "DeliveryStatus",
         action = "Get"
         orderId = RouteParameter.Optional
     }
);

在 Controller 中:

[HttpGet]
[ResponseType(typeof(DeliveryStatusDto))]
public dynamic Get(string deliveryType, Guid? orderId = null) { ... }

确保路由表不会猜测操作映射到哪条路由。

关于c# - Swashbuckle/Swagger 没有从配置中获取路由,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37965970/

相关文章:

c# - 是否可以为 ASP.NET MVC 路由提供比其他路由更多的服务器资源?

asp.net - 如何使用 web.config 文件保护页面?

swagger - 如何将 swagger 2.0 JSON 文件分解为多个模块

asp.net-core - Swashbuckle aspnet core 2.0 Swaggerconfig.cs 未创建

c# - 共享类与 swagger 生成的类

c# - BHO 暴露 javascript 方法在 IE 9+ 中有效,但在早期版本中失败

c# - 正则表达式重音不敏感?

c# - 有没有一种巧妙、有效的方法可以用两个不同的参数调用同一个方法两次?

c# - 在 C# .net 后面的代码中从我的 SqlDataSource 访问数据

c# - 在浏览器关闭时运行服务器端功能