小编Lee*_*Lee的帖子

可以在Asp.Net Web Api路由中使用句点吗?

我正在从原始的http处理程序移动API项目,我在路径中使用句点:

http://server/collection/id.format
Run Code Online (Sandbox Code Playgroud)

我想在Web Api(自托管)版本中遵循相同的URL架构,并尝试这样做:

var c = new HttpSelfHostConfiguration(b);
c.Routes.MapHttpRoute(
    name: "DefaultApiRoute",
    routeTemplate: "{controller}/{id}.{format}",
    defaults: new { id = RouteParameter.Optional, format = RouteParameter.Optional },
    constraints: null
);
Run Code Online (Sandbox Code Playgroud)

不幸的是,这似乎没有解决(在/ foo,/ foo/bar和/foo/bar.txt上一致的404').在'format'之前使用斜杠的类似模式工作正常:

var c = new HttpSelfHostConfiguration(b);
c.Routes.MapHttpRoute(
    name: "DefaultApiRoute",
    routeTemplate: "{controller}/{id}/{format}",
    defaults: new { id = RouteParameter.Optional, format = RouteParameter.Optional },
    constraints: null
);
Run Code Online (Sandbox Code Playgroud)

我还没有深入研究Web Api的代码,在此之前我想过我会在这里询问这是否是Web Api中已知的,甚至是合理的限制.

更新:我忽略了提到"id"和"format"是字符串,这对于解决这个问题很重要.添加约束以从"id"标记中排除句点可解决404问题.

c# asp.net-mvc asp.net-mvc-routing asp.net-web-api

18
推荐指数
4
解决办法
1万
查看次数