网页API与多个签名相同的路由得到 [英] Web Api multiple get with same signature routing

查看:87
本文介绍了网页API与多个签名相同的路由得到的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我要建有多个GET / POST调用具有相同签名的Web API。现在我知道,在多个相同的呼叫的情况下,通常有两个选择:分成不同的控制器,或在您的路由使用{}行动。我已经在{}动作方法,因为它最适合我相信我的大部分控制器。然而,在我的控制器之一,我会preFER不使用操作方法。

I'm building a web api that has multiple get/post calls that have the same signatures. Now I know that in the case of multiple identical calls, you generally have 2 options: separate into different controllers, or use {action} in your routes. I have gone the {action} method as it fits best I believe in most of my controllers. However, in one of my controllers I would prefer not to use the action method.

我有一个电话就像这样:

I have a call like so:

[HttpGet]
public Program Program(string venue, string eventId)
//api/{controller}/{venue}/{eventId}

现在我需要一个新的呼叫

Now I need a new call

[HttpGet]
public Program ProgramStartTime(string venue, string eventId)
//api/{controller}/{venue}/{eventId}

我知道我可以添加一个动作名称本和呼叫即

I know I can add an action name to this and call i.e

api/{controller}/{action}/{venue}/{eventId}

但我觉得它打破了预期。有没有一种方法,我可以有些类似

But I feel like it breaks the expected. Is there a way that I could some something like

api/Content/LAA/1/PST
api/Content/LAA/1?PST

另外,如果我必须去行动路线,我现在已经有了我用其他控制器的路线,但它只是使用(编号)作为其唯一的参数。将与此一个新的路由冲突?有没有建立一个更好的办法我的路线?

Also if I have to go the action route, I currently already have a route I use for other controllers, but it simply uses {id} as its only parameter. Will a new route conflict with this one? Is there a better way to setup my routes?

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

config.Routes.MapHttpRoute(
   name: "...",
   routeTemplate: "api/{controller}/{action}/{venue}/{eventId}/{...}/{***}/{###}",
   defaults: new {### = RouteParameter.Optional}
);

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

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

我期望这将有多达5个参数的至少一种方法

I expect at least one method that would have up to 5 parameters

推荐答案

下面是我找到了答案,它不会pretty很多正是我想要的:

Here's the answer I found and it does pretty much exactly what I wanted:

        config.Routes.MapHttpRoute(
            name: "VenuesAllOrStream",
            routeTemplate: "api/Racing/{action}",
            defaults: new { controller = "Racing", action = "Venues" },
            constraints: new { action = "Venues|All|Streaming" }
        );

        config.Routes.MapHttpRoute(
            name: "VenueOrVideo",
            routeTemplate: "api/Racing/{venue}/{action}",
            defaults: new { controller = "Racing", action = "RaceNumbers" },
            constraints: new { action = "RaceNumbers|Video" }
        );

        config.Routes.MapHttpRoute(
            name: "ProgramOrMtp",
            routeTemplate: "api/Racing/{venue}/{race}/{action}",
            defaults: new { controller = "Racing", action = "Program" },
            constraints: new { action = "Program|Mtp", race = @"\d+" }
        );

重要的是,该VenuesAllOrStream是首先作为否则VenueOrVideo拿起路由。我极有可能会解压出来的动作限制为枚举更高版本。

It is important that the VenuesAllOrStream is first as otherwise the VenueOrVideo picks up the route. I most likely will extract out the action constraints into enums later.

简要说明:设置默认的行动使得该航线基本上使它成为可选参数。因此,每个路由作品,未经{}行动实际上被设置。

Brief note : Setting the action default allows for the route to basically make it an optional parameter. So each route works without the {action} actually being set.

这篇关于网页API与多个签名相同的路由得到的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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