gpt4 book ai didi

asp.net-mvc-routing - ASP.NET 4 MVC Web API : Documentation for complex routing

转载 作者:行者123 更新时间:2023-12-04 00:03:32 24 4
gpt4 key购买 nike

web api 似乎只适用于标准用例。
但我想做更复杂的路由,但找不到复杂路由的文档。
如果我有更多的 Controller ,路由会变得越来越复杂。

我可以定义几个具有依赖关系的可选参数吗?
像这样:

/api/document/{par1}/{par2}

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/...}

是否有关于 web api 路由的详细文档?微软的教程太基础了...
我需要更多关于路由的信息。

我有两个 Controller 和一些麻烦,因为两个路由非常相似,所以走错了路由。我可以使用 [Action]-Attribute 作为一种解决方法,但这感觉不对......我还必须考虑路由的顺序。这是合乎逻辑的,但没有提及。
web api仅用于简单的rest 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" }
// );

我有两种方法:
[AcceptVerbs("get")]
public HttpResponseMessage Get(int id)

[ActionName("file")]
[AcceptVerbs("get")]
public HttpResponseMessage filedownload(int id)

现在我遇到了问题,即使我注释掉第二条路线,也会触发文件操作,并且不会触发正常的获取特定文档方法,因为多个操作...我尝试了 [NoAction] 属性,但这不起作用.. .
但是为什么在route-template中没有action会触发file-method呢? (或者如果第二条路由是事件的,为什么url中没有action就不会触发正常的get-document方法....)
我目前的解决方法是为所有其他方法设置默认操作,但这不是一个好的解决方案。

最佳答案

您可以使用路由约束在 global.asax 中设置路由条件,例如:

routes.MapRoute(
"ApiRoute",
"api/document/{par1}/{par2}",
new {controller="Document", action="SomeMethod"},
new {par1 = @"\d+" }
);

在最后一个参数中,您可以指定必须与要使用的路由的指定参数匹配的正则表达式。在上面的例子中 par1仅用于数字,但您可以使用任何正则表达式,例如:

routes.MapRoute(
"ApiRoute",
"api/document/{par1}/{par2}",
new {controller="Document", action="SomeMethod"},
new {par1 = @"(value1|value2|value3)" }
);

关于asp.net-mvc-routing - ASP.NET 4 MVC Web API : Documentation for complex routing,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10993017/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com