Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/320.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/asp.net/35.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 虚张声势/虚张声势不从配置中拾取路线_C#_Asp.net_Swagger_Swagger Ui_Swashbuckle - Fatal编程技术网

C# 虚张声势/虚张声势不从配置中拾取路线

C# 虚张声势/虚张声势不从配置中拾取路线,c#,asp.net,swagger,swagger-ui,swashbuckle,C#,Asp.net,Swagger,Swagger Ui,Swashbuckle,我使用的是Swashback(围绕swagger的.NET包装) 当我生成swagger文档时,它会根据控制器名称/参数生成文档中的URI,而不是应用程序启动时在配置中定义的已定义路由 所以我有一个控制器: namespace Pat.PostingService.Api.Version1 { public class DeliveryStatusController : ApiController { [ResponseType(typeof(Delivery

我使用的是Swashback(围绕swagger的.NET包装)

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

所以我有一个控制器:

namespace Pat.PostingService.Api.Version1
{
    public class DeliveryStatusController : ApiController
    {
         [ResponseType(typeof(DeliveryStatusDto))]
         public dynamic Get(string deliveryType, Guid? orderId = null)
         {
             ...
         }
    }
{
[HttpGet]
[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

与我的同事一起,我们相信,
\u apiExplorer.apisdescriptions
(来自swashback.Core中SwaggerGenerator.cs使用的System.Web.Http.Description.IApiExplorer)正在返回错误的路径,尽管我们可能是错的

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


编辑:

我们也在使用SDammann进行版本控制(想象版本2文件夹中的第二个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 }
    );
}
如果你不反对使用,你可以改为

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
     }
);
在控制器中:

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

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

感谢Nkosi的回答-我可能应该把它放在那里,但我们确实在底部有最一般的路由(我尝试重新排列只是为了检查)。同样,我们尝试了基于属性的路由,但是我们正在使用SDammann进行版本控制,它不支持基于属性的路由,因此我们需要Swashback来支持启动路由配置。您应该使用这些详细信息更新问题,以便其他人能够更好地了解整个情况。您是否尝试手动设置
action=“Get”
在路线映射的默认值中。ie
默认值:新建{controller=“DeliveryStatus”,action=“Get”,orderId=RouteParameter.Optional}
。以及
HttpGet
属性,以确保不会猜测操作属于哪个路由?恐怕这并没有解决问题:(这很奇怪,我刚刚测试了您的代码,得到了一个完美的结果……它将端点显示为/SpecialDeliveryServiceStatus/{deliveryType}/{orderId}。尝试创建一个ISchemaFilter并检查APIsDescription-如果愿意,您可以在此处更改路径,尽管这需要大量静态代码和大量操作ID(如果这是您唯一的端点,则不会有问题)