使用多个命名空间属性时,从路由的路由表丢失 [英] Attribute routes missing from routing table when using multiple namespaces

查看:172
本文介绍了使用多个命名空间属性时,从路由的路由表丢失的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我用我的网络API(4.0)项目名称空间的版本。我使用路由属性来定义我们的行动定制路由。这只是正常时,控制器只在一个命名空间/版本存在。

I am using namespace versioning in my web api (4.0) project. I'm using Attribute routing to define custom routes for our actions. This works just fine when a controller only exists in one namespace/version.

但是,当控制器在两个命名空间/版本存在,属性路线完全忽略。我使用的解决方案,<一个href=\"http://stackoverflow.com/questions/29337064/get-a-list-of-attribute-route-templates-asp-net-webapi-2-2\">here看什么航线在路由表中。而且只包含一个途径,我的单一的控制器。如果我删除/注释掉/改变V2控制器的名称,然后突然双节控制器的路线出现在路由表中也是如此。

But when a controller exists in both namespaces/versions, the attribute route is ignored completely. I am using the solution here to look at what routes are in the routing table. And it only contains one route, for my "single" controller. If I delete/comment out/change the name of the controller in V2, then suddenly the "double" controller's route appears in the routing table as well.

有什么理由因为同一类/控制器名称在2个不同的命名空间存在的属性路线将被忽略?这里是我的code,分解到最简单的失败例子:

Is there any reason that an attribute route would be ignored because the same class/controller name exists in 2 different namespaces? Here is my code, broken down to the simplest failing example:

namespace API.v1
{
    public class SingleController : ApiController
    {
        [HttpGet]
        [Route("api/{version}/Single")]
        public string Test()
        {
            return "Response";
        }
    }
}

本类/路由工作得很好。 的http://本地主机:57560 / API / V1 /单返回响应

This class/route works just fine. http://localhost:57560/api/v1/Single returns "Response".

namespace API.v1
{
    public class DoubleController : ApiController
    {
        [HttpGet]
        [Route("api/{version}/Double")]
        public string Test()
        {
            return "Response";
        }
    }
}

namespace API.v2
{
    public class DoubleController : ApiController
    {
        [HttpGet]
        [Route("api/{version}/Double")]
        public string Test()
        {
            return "Response";
        }
    }
}

这2类失败;属性路由从路由表中失踪,的http://本地主机:57560 / API / V1 /双人间返回404,以及的http://本地主机:57560 / API / V2 /双人间。如果我只是改变了v2.Double类的名称,那么它工作得很好。

These 2 classes fail; the attribute route is missing from the routing table, and http://localhost:57560/api/v1/Double returns 404, as well as http://localhost:57560/api/v2/Double. If I just change the name of the v2.Double class, then it works just fine.

在这个例子中,我没有默认路由设置在所有;只有属性的路由。当使用缺省路由,版本控制和路由的工作就好了:

In this example, I have no default routes set up at all; only the Attribute routing. When using default routes, the versioning and routing work just fine:

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

这条路线完美的作品跨越多个版本。但我需要它与属性的路由工作。

This route works perfectly across multiple versions. But I need it to work with attribute routing.

推荐答案

我已经能够在这里使用的解决方案来解决这个问题:的 http://abhinawblog.blogspot.com/2014/12/web-api-versioning-using.html 。这个问题似乎与该使用的缺省路由表路由属性不同的方式。它要求路由具有唯一的名称,并在路由的版本号已经把他们的自定义限制。

I have been able to solve the issue using the solution here: http://abhinawblog.blogspot.com/2014/12/web-api-versioning-using.html. The problem appears to be related to the way that attribute routing differs from using the default routes table. It is required that the routes have unique names, and that the version numbers in the routes have custom constraints put on them.

这篇关于使用多个命名空间属性时,从路由的路由表丢失的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆