,但有些事很难处理的,如资源之间存在嵌套关系:客户包含订单,书有作者属性等等。对于这种Uri,我们希望的路由是这样的:或
[路线(“客户/{customerId}/订单”)]public IEnumerable, GetOrdersByCustomer (int customerId), {,,,}
,
引用>
,
using System.Web.Http; namespace WebApplication {,,,public static class WebApiConfig ,,,{,,,,,,,public static void 注册(HttpConfiguration 配置) ,,,,,,,{,,,,,,,,,,,//,Web API routes ,,,,,,,,,,, config.MapHttpAttributeRoutes (),,,,,,,,,,,,,//, Other Web API configuration not 只显示,,,,,,,} ,,,} }
,
protected void Application_Start () { ,,,WebApiConfig.Register (GlobalConfiguration.Configuration);,,,,//瓆,
protected void Application_Start () { ,,,GlobalConfiguration.Configure (WebApiConfig.Register);,,,,//瓆,
,
,
<强> 强>
<李>
李> <李>
李> <李>
李>
<李>
李> <李>
李> <李>
李>
<代码> 代码>
,
,
[路线(“用户/{id: int}] public User GetUserById (int id), {,,,} [路线(“用户/{名称}”]public User GetUserByName (string 名称),{,,,},
约束介绍示例α匹配大写或小写字母(a - z、a - z) {x:α}bool
{x: bool} datetime
{x: datetime}十进制
{x:小数}双
{x:双}浮动匹配一个32位浮点数{x:浮动}guid
{x: guid} int
{x: int}长度匹配一个长度在指定范围内的字符串长
{x:长}马克斯·匹配指定了最大值的整数{x: max(10)}最大长度匹配指定了最大长度字符串{x:最大长度(10)}分钟匹配指定了最小值的整数{x: min(10)}最小长度匹配指定了最小长度字符串{x:最小长度(10)}范围匹配指定了大小区间的整数{x:范围(10、50)}正则表达式匹配一个正则表达式{x:正则表达式(^ \ d {3} - \ d {3} \ d {4} $)},如果要指定多个约束,需要用冒号间隔”代码>
<代码>接口,还可以创建自定义路由约束。(不过一般正则就可以搞定了)代码>