Web API路由,两个路由匹配

时间:2013-08-09 14:10:49

标签: c# asp.net-web-api attributerouting

我在ApiController中设置了以下两项操作:

[POST("api/admin/image/{id}/upload")]
[Authorize(Roles = RoleNames.Administrator)]
public Foo UploadVersion(int id)

public Bar Post(int id, Baz x) {}

鉴于此路由配置,它们是冲突的:

context.Routes.MapHttpRoute(
    name: "Admin_Api",
    routeTemplate: "api/admin/{controller}/{id}",
    defaults: new 
        {
             id = RouteParameter.Optional
        });

我的猜测是UploadVersion也通过路线映射得到某种默认路线,但我该如何防止呢?

如果有人想知道我想要做什么:

POST /api/admin/image/{id}/upload -> UploadVersion()
POST /api/admin/image/{id}        -> Post()

1 个答案:

答案 0 :(得分:1)

根据您设置的路由,它们是相同的路由,无法区分它们。你每次都会得到这条路线:

POST /api/admin/image/{id}        -> Post()

尝试修改路线,使其如下所示:

POST /api/admin/image/upload/{id} -> UploadVersion()
POST /api/admin/image/{id}        -> Post()