Asp.net web api WebApi忽略管线定义模板中的默认值

Asp.net web api WebApi忽略管线定义模板中的默认值,asp.net-web-api,asp.net-web-api2,asp.net-web-api-routing,Asp.net Web Api,Asp.net Web Api2,Asp.net Web Api Routing,我正在使用WebApi路由解决方案,对于路由模板中未提供令牌值的路由,我希望使用默认值自动填充它们 我的路线声明: config.Routes.MapHttpRoute( name: "LandingPage Route", routeTemplate: "api/{version}/{controller}/{id}", defaults: new { controller =

我正在使用WebApi路由解决方案,对于路由模板中未提供令牌值的路由,我希望使用默认值自动填充它们

我的路线声明:

config.Routes.MapHttpRoute(
           name: "LandingPage Route",
           routeTemplate: "api/{version}/{controller}/{id}",
           defaults: new
           {
               controller = "LandingPageCommon",
               id = System.Web.Http.RouteParameter.Optional,
               version = ApiVersions.None,
               logging = true
           },
           constraints: new
           {
               version = new ApiVersionRouteConstraint()
           }
       //  handler: apiVersionHandler
       );
现在我通过邮递员打了两个电话:

1) 为GET/api/v3/LandingPageCommon/886858按预期工作(我得到了适当的实体)

  • {version}->v3
  • {controller}->LandingPageCommon
  • {id}->886858
2) 不适用于GET/api/LandingPageCommon/886858

  • {version}->未提供,因此应采用声明中的“默认值”->“无”的形式
  • {controller}->LandingPageCommon
  • {id}->886858
我已经创建了自定义RouteConstraint来保护{version}参数。在调试第二个调用时,我发现路由令牌没有正确设置({version}->{controller}和{controller}->{id}):

在sime时,我在路由声明中看到了默认值:

config.Routes.MapHttpRoute(
           name: "LandingPage Route",
           routeTemplate: "api/{version}/{controller}/{id}",
           defaults: new
           {
               controller = "LandingPageCommon",
               id = System.Web.Http.RouteParameter.Optional,
               version = ApiVersions.None,
               logging = true
           },
           constraints: new
           {
               version = new ApiVersionRouteConstraint()
           }
       //  handler: apiVersionHandler
       );

为什么第二次调用的{version}未设置为其默认值?
谢谢

问题是引擎如何知道在第二种情况下调用哪个路由,我认为这不是您正在寻找的可行路由,在路由之间不能有默认值。检查这个关于MVC路由的答案,但同样的规则也适用于API路由。。你指向了正确的方向。感谢Prashant,问题是引擎如何知道在第二个场景中调用哪个路由,我认为这不是您正在寻找的可行路由,在路由之间不能有默认值。检查这个关于MVC路由的答案,但同样的规则也适用于API路由。。你指向了正确的方向。谢谢你,普拉尚特