use*_*899 5 asp.net-mvc-routing asp.net-web-api
web api似乎只适用于标准用例.但我想做更复杂的路由,但找不到复杂路由的文档.如果我有更多的控制器,路由变得越来越复杂.
我可以使用依赖项定义几个可选参数吗?像这样:
/api/document/{par1}/{par2}
Run Code Online (Sandbox Code Playgroud)
par1和par2应该是可选的,但par2应仅在par1存在时匹配.
并且递归参数可能吗?
/api/documents/characteristics/{round/green/red-dots}
/api/documents/characteristics/{square/yellow}
/api/documents/characteristics/{square/yellow/flat}
/api/documents/characteristics/{square/yellow/flat/...}
Run Code Online (Sandbox Code Playgroud)
是否有关于web api路由的详细文档?微软教程太基础了...我需要有关路由的更多信息.
我有两个控制器和一些麻烦,因为两个路由非常相似,所以采取了错误的路线.我可以使用[Action] -Attribute作为解决方法,但这感觉不对......我还必须考虑路由的顺序.这是合乎逻辑的,但未提及.web api只适用于简单的休息api吗?
编辑: 我试过这个:
routes.MapHttpRoute(
name: "DefaultApi",
routeTemplate: "api/{mandant}/documents/{id}",
defaults: new { controller = "documents", id = RouteParameter.Optional }
);
//routes.MapHttpRoute(
// name: "DefaultApiWithAction",
// routeTemplate: "api/{mandant}/documents/{id}/{action}",
// defaults: new { controller = "documents" }
// );
Run Code Online (Sandbox Code Playgroud)
我有两种方法:
[AcceptVerbs("get")]
public HttpResponseMessage Get(int id)
[ActionName("file")]
[AcceptVerbs("get")]
public HttpResponseMessage filedownload(int id)
Run Code Online (Sandbox Code Playgroud)
现在我遇到了问题,即使我注释掉第二条路线也触发了文件操作,并且由于多个动作而没有触发正常的获取特定文档方法...我尝试了[NoAction]属性但这不起作用..但是如果路由模板中没有动作,为什么会触发文件方法呢?(或者如果第二个路由处于活动状态,为什么如果url中没有动作,则不会触发正常的get-document方法....)我当前的解决方法是为所有其他方法设置default-action,但是这不是一个好的解决方案.
您可以使用路由约束在global.asax中设置路由条件,如:
routes.MapRoute(
"ApiRoute",
"api/document/{par1}/{par2}",
new {controller="Document", action="SomeMethod"},
new {par1 = @"\d+" }
);
Run Code Online (Sandbox Code Playgroud)
在最后一个参数中,您可以指定必须与要使用的路由的指定参数匹配的正则表达式.在上面的示例中par1仅用于数字,但您可以使用任何正则表达式,如:
routes.MapRoute(
"ApiRoute",
"api/document/{par1}/{par2}",
new {controller="Document", action="SomeMethod"},
new {par1 = @"(value1|value2|value3)" }
);
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
8171 次 |
| 最近记录: |