基于查询字符串参数名称的路由

时间:2012-09-27 10:44:41

标签: c# asp.net-web-api query-string asp.net-web-api-routing

我正在尝试在我的MVC4 WebAPI项目中配置路由。

我希望能够根据名称或类型搜索产品,如下所示:

/api/products?name=WidgetX - 返回名为WidgetX的所有产品 /api/products?type=gadget - 返回小工具类型

的所有产品

路由配置如下:

config.Routes.MapHttpRoute(
    name: "Get by name",
    routeTemplate: "api/products/{name}",
    defaults: new { controller = "ProductSearchApi", action = "GetProductsByName", name = string.Empty }
);

config.Routes.MapHttpRoute(
    name: "Get by type",
    routeTemplate: "api/products/{type}",
    defaults: new { controller = "ProductSearchApi", action = "GetProductsByType", type = string.Empty }
);

问题是查询字符串参数的名称似乎被忽略,因此无论查询字符串参数的名称如何,第一个路径始终是使用的路径。 如何修改我的路线以使其正确?

4 个答案:

答案 0 :(得分:30)

您需要的只是下面的一条路线,因为查询字符串不用作路由参数:

config.Routes.MapHttpRoute(
    name: "Get Products",
    routeTemplate: "api/products",
    defaults: new { controller = "ProductSearchApi" }
);

然后,定义两个方法,如下所示:

GetProductsByName(string name)
{}

GetProductsByType(string type)
{}

路由机制 smart 足以根据查询字符串的名称将您的网址路由到正确的操作,无论输入参数是否相同。当然,所有带前缀的方法都是Get

您可能需要阅读此内容: http://www.asp.net/web-api/overview/web-api-routing-and-actions/routing-and-action-selection

答案 1 :(得分:4)

您不需要在路线中包含查询参数。应该只有一个简单的路由映射来覆盖所有ApiControllers上的Http方法:

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

您需要调整路径的唯一时间是您要将参数移动到实际路径中,而您似乎并未执行此操作。然后,您要通过两个字段搜索的GET http方法将是:

public IEnumerable<Product> Get(string name, string type){
    //..your code will have to deal with nulls of each parameter
}

如果您想一次只按一个字段进行搜索,那么您应该考虑将不同的控制器用于不同的目的。即,SearchProductByTypeController具有单个Get(string type)方法。然后路由是/ api / SearchProductByTypeController?type = gadget

答案 2 :(得分:0)

尝试更改string.Empty

RouteParameter.Optional

答案 3 :(得分:0)

你确定控制器没问题吗?我的意思是,参数的名称。

    public string GetProductsByName(string name)
    {
        return "Requested name: " + name;
    }

    public string GetProductsByType(string type)
    {
        return "Requested type: " + type;
    }
相关问题