ASP.NET MVC 路由的无限 URL 参数
我需要一个可以在 ASP.NET 控制器上获取无限参数的实现。如果我给你一个例子会更好:
假设我将有以下网址:
example.com/tag/poo/bar/poobar
example.com/tag/poo/bar/poobar/poo2/poo4
example.com/tag/poo/bar/poobar/poo89
如你所见,在 example.com/tag/
之后它将获得无限数量的标签,并且斜杠将是这里有一个分隔符。
在控制器上我想这样做:
foreach(string item in paramaters) {
//this is one of the url paramaters
string poo = item;
}
有什么已知的方法可以实现这一点吗?如何从控制器获取值?使用 Dictionary
或 List
?
注意:
在我看来,这个问题没有得到很好的解释,但我尽力了。 in.随意调整它
I need an implementation where I can get infinite parameters on my ASP.NET Controller. It will be better if I give you an example :
Let's assume that I will have following urls :
example.com/tag/poo/bar/poobar
example.com/tag/poo/bar/poobar/poo2/poo4
example.com/tag/poo/bar/poobar/poo89
As you can see, it will get infinite number of tags after example.com/tag/
and slash will be a delimiter here.
On the controller I would like to do this :
foreach(string item in paramaters) {
//this is one of the url paramaters
string poo = item;
}
Is there any known way to achieve this? How can I get reach the values from controller? With Dictionary<string, string>
or List<string>
?
NOTE :
The question is not well explained IMO but I tried my best to fit it.
in. Feel free to tweak it
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
像这样:
Like this:
catch all 将为您提供原始字符串。如果您想要一种更优雅的方式来处理数据,您始终可以使用自定义路由处理程序。
注册路由处理程序。
在控制器中以数组形式获取标签。
The catch all will give you the raw string. If you want a more elegant way to handle the data, you could always use a custom route handler.
Register the route handler.
Get the tags as a array in the controller.
这称为catch-all:
That's called catch-all:
以防万一有人在 .NET 4.0 中使用 MVC,您需要小心定义路由的位置。我很高兴去
global.asax
并按照这些答案(以及其他教程)中的建议添加路线,但一无所获。我的路线都默认为{controller}/{action}/{id}
。在 URL 中添加更多分段会导致 404 错误。然后我在App_Start文件夹中发现了RouteConfig.cs文件。事实证明,该文件是由Application_Start()
方法中的global.asax
调用的。因此,在 .NET 4.0 中,请确保在那里添加自定义路由。 这篇文章很好地涵盖了它。Just in case anyone is coming to this with MVC in .NET 4.0, you need to be careful where you define your routes. I was happily going to
global.asax
and adding routes as suggested in these answers (and in other tutorials) and getting nowhere. My routes all just defaulted to{controller}/{action}/{id}
. Adding further segments to the URL gave me a 404 error. Then I discovered the RouteConfig.cs file in the App_Start folder. It turns out this file is called byglobal.asax
in theApplication_Start()
method. So, in .NET 4.0, make sure you add your custom routes there. This article covers it beautifully.在 asp .net core 中,您可以在路由中使用 *
例如
[HTTPGet({*id})]
此代码可以使用多个参数,或者在使用带斜杠的发送字符串时使用它们来获取所有参数
in asp .net core you can use * in routing
for example
[HTTPGet({*id})]
this code can multi parameter or when using send string with slash use them to get all parameters