1

我有 WebAPI url( http://localhost:5134/api/TechDisci/00026),它为 user 返回多个 TechDisciplines 00026/TechDisci/是控制器名称,Get方法返回 JSON 数据。

JSON 数据在结果集中具有主要值和次要值。现在用户想用这样的 url 来primary过滤secondary

http://localhost:5134/api/TechDisci/00026/primaryhttp://localhost:5134/api/TechDisci/00026/secondary

我的 Java 家伙能够处理这种 url 的事情。如何在 WebAPI 中处理相同的问题?

4

1 回答 1

1

You can have "primary" and "secondary" as action (method) names in the controller. Then you will need to add a new route (in class Application.WebApiConfig).

config.Routes.MapHttpRoute(
    name: "DefaultApi",
    routeTemplate: "api/{controller}/{id}/{action}"
);

Or if "primary" and "secondary" are parameters to the same method. Then you might add this route

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

In this case you will need to add an overload to your Get method that accepts two parameters (id, priority)

In both cases add the route before the main route, just in case to make sure it is the first one that has a match.

于 2013-10-17T23:36:54.257 回答