查询参数路由约束 [英] Query parameter route constraints

查看:176
本文介绍了查询参数路由约束的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我刚开始使用的ASP.NET Web API 2.1和所遇到的限制。使用属性的路由,我能做到以下几点:

I just started using ASP.NET Web API 2.1 and have encountered a limitation. Using Attribute Routing, I can do the following:

[Route("item/{id:int}")]
public IHttpActionResult GetItem(int id)
{
    ...
}

该网址 /项/ 5 将被路由到这个动作,但网址 /项目/ ABC 将没有,因为 INT 约束的 {ID:INT}

The URL /item/5 will be routed to this action, but the URL /item/abc will not, because of the int constraint in {id:int}.

我试图改变我的网址,使 ID 参数是与它的约束下查询字符串,尽管从未被提及或展示了使用上的查询参数路由约束在文档中

I tried changing my URL so that the id parameter was in the query string along with its constraint, despite the use of route constraints on query parameters never being mentioned or demonstrated in the documentation.

[Route("item?{id:int}")]
public IHttpActionResult GetItem(int id)
{
    ...
}

如果我现在尝试跑,我得在的Application_Start 配置方法调用错误。

If I try to run now, I get an error on the Configure method call in Application_Start.

protected void Application_Start()
{
    GlobalConfiguration.Configure(WebApiConfig.Register);
}

该消息如下:

ArgumentException的是由用户code

ArgumentException was unhandled by user code

路由模板不能以/或〜字符开始,并且不能包含一个?性格。

The route template cannot start with a '/' or '~' character and it cannot contain a '?' character.

两件事情打扰我这个问题。

Two things bother me about this.

首先,<一个href=\"http://www.asp.net/web-api/overview/web-api-routing-and-actions/attribute-routing-in-web-api-2#$p$pfixes\"相对=nofollow>部分记录路线prefixes 的MSDN上明确指出放置字符在路径模板的起点是完全可以接受。我试了一下,它可以作为记录。

First, the section documenting Route Prefixes on MSDN makes clear that placing a ~ character at the start of the route template is completely acceptable. I tried it, and it works as documented.

第二,如果不是这样,我怎么能放置在查询参数的路由约束?考虑下面的,与路由约束删除。

Second, if not like this, how can I place a route constraint on a query parameter? Consider the following, with the route constraint removed.

[Route("item")]
public IHttpActionResult GetItem(int id)
{
    ...
}

该网址 /项/ 5 将被路由到这一行动, ID 设置为 5 - 但因此将网址 /项目/ ABC ID 集到 0

The URL /item/5 will be routed to this action, with id set to 5 - but so will the URL /item/abc, with id set to 0.

请问有没有办法把一个路由约束的查询参数?

Is there no way to place a route constraint on a query parameter?

推荐答案

根据的http:/ /attributerouting.net/#asp-net-web-api 这是不可能的。

*当心!由于与Web API的虚拟主机提供商框架集成的问题,下面的功能将无法正常工作:

*Beware! Due to integration issues with the Web API WebHost framework, the following features will not work:

•查询参数的限制,*

/迈克尔

这篇关于查询参数路由约束的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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