路线为特定的控制器 [英] Routes for specific controllers

查看:109
本文介绍了路线为特定的控制器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的Web API的情况下,这个(默认)路由非常适用于几乎所有的控制器。

In my Web API scenario, this (default) route works well for almost all my controllers.

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

和一对夫妇我的控制器,我想有他们因动作名称映射,像这样的:

and for a couple of my controllers, I want to have them mapped by their action name, like this:

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

背景:

我的大多数控制器是默认情况下GEE,POST映射,PUT和DELETE。
但是,我保留了一些admin的控制器,执行操作,如数据初始化,清理和测试的东西。这些控制器可以有多种方法,它通过GET是excetude,因此第二条路线。

Most of my controllers are to be mapped by default to GEE, POST, PUT and DELETE. However, I reserve some "admin" controllers that perform operations, such as data initialization, cleanup and testing stuff. These controllers may have multiple methods that are excetude via GET, hence the second route.

如果我用这些途径,他们的工作很好,但是,如果我同时设置,我得到两个故障之一:

If I use either of these routes, they work well, however, if I set both, I get one of two failures:

默认路由第一:

api/books/                -> OK
api/books/someGuidId      -> OK
api/admin/someAdminAction -> 500 Internal Server Error (multiple actions found)
api/test/sometestAction   -> 500 Internal Server Error (multiple actions found)

行动路线第一:

api/books/                -> OK
api/books/someGuidId      -> 404 Not Found (no action that matches "someGuidId")
api/admin/someAdminAction -> OK
api/test/sometestAction   -> OK

我的问题是:我怎么能有这些线路在同一时间制定

My question is: how can I have these routes working out at the same time?

[修改]

API /书籍/ someGuidId不是一个重要的电话,我可以没有它(通过GET)但是,POST,PUT和DELETE以相同的URL会失败,至极是不能接受的。

api/books/someGuidId is not an important call, I can live without it (via GET) however, POST, PUT and DELETE to same url will fail, wich is not acceptable.

推荐答案

在击溃的顺序关系,所以你应该首先设置更加具体击溃:

The order of the routs matter, so You should set more specific routs first:

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

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

编辑:

默认航线第一:

Default route first:

api/books/

此网址配衬第一模式:API / {控制器} / {ID}结果
框架查找图书控制器 GET (PUT,POST,DELETE)方法,并发现它。这种方法被调用默认 ID 值。

This url matchs the first pattern: api/{controller}/{id}.
Framework looks for books controller with GET (PUT,POST,DELETE) method, and finds it. This method is invoked with default id value.

api/admin/someAdminAction  

这也网址配衬第一模式。 (顺便说一句,这不要紧,你如何命名您的路由定义参数: ID superParamName ,当框架以击溃URL进行比较,只会寻找模式,不能由参数名称区分开来。

This url also matchs the first pattern. (BTW, It doesn't matter how You name Your parameters in the route definition: id or superParamName, When Framework compares URL with routs it will look only for patterns and can not distinguish by param names.

该框架将尝试调用 GET (PUT,POST,DELETE)管理​​控制器与方法 someAdminAction ID(其中,我想,是不是值有效身份证:)

The Framework will try to invoke GET (PUT,POST,DELETE) method of admin controller with someAdminAction id (Which, I suppose, is not a valid id value:)

类似的事情时,你首先要定义动作路线你得到:

Similar thing You get when You define Action route first:

api/books/   

马成顺的 API / {控制器} / {ID} 的,ID可以省略,因为它被设置为可选。

Matchs api/{controller}/{id}, id can be omitted, cause it was set to "optional".

api/books/someGuidId

马成顺的 API / {控制器} / {行动} / {ID} 的,因此,当框架查找控制器someGuidId 操作方法,并没有找到它,它抛出一个异常。 404未找​​到(不采取行动符合someGuidId)

Matchs api/{controller}/{action}/{id}, so, when Framework looks for someGuidId action method in book controller and it doesn't find it, it throws an exception. 404 Not Found (no action that matches "someGuidId")

编辑2:我想,如果你将永远传递 ID 参数为操作和地方操作路线首先,不会得到任何冲突。试试这个。结果
如果你需要 ID 参数在这两种情况下可选的,你可以从两个击溃删除它,然后通过(问号)。

Edit 2: I think, in case You will always pass id param for "Actions" and place "Actions" route first, You will not get any collisions. Try this.
If You need id param be optional in both cases, You can just remove it from both routs and pass it in a normal way via ?(question mark).

其中之一,这将解决您的路由问题。

One of this would fix Your routing problem.

这篇关于路线为特定的控制器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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