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

问题描述

我用我的网络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.

但是,当控制器在两个命名空间/版本存在,属性路线完全忽略。我使用的解决方案,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";
        }
    }
}

本类/路由工作得很好。 返回响应

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类失败;属性路由从路由表中失踪,返回404,以及。这个问题似乎与该使用的缺省路由表路由属性不同的方式。它要求路由具有唯一的名称,并在路由的版本号已经把他们的自定义限制。

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.

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

07-31 11:01