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 :
我需要一个实现,在这个实现中,我可以在ASP中获得无限的参数。网络控制器。如果我给你举个例子会更好:
Let's assume that I will have following urls :
假设我有以下url:
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.
正如您所看到的,在example.com/tag/和slash之后,它将获得无限数量的标签。
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
这个问题在我看来没有很好的解释,但我尽力去适应它。在。请随意调整
4 个解决方案
#1
55
Like this:
是这样的:
routes.MapRoute("Name", "tag/{*tags}", new { controller = ..., action = ... });
ActionResult MyAction(string tags) {
foreach(string tag in tags.Split("/")) {
...
}
}
#2
26
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.
所有的捕获都将给你原始的字符串。如果您想要一种更优雅的方法来处理数据,您可以始终使用自定义路由处理程序。
public class AllPathRouteHandler : MvcRouteHandler
{
private readonly string key;
public AllPathRouteHandler(string key)
{
this.key = key;
}
protected override IHttpHandler GetHttpHandler(RequestContext requestContext)
{
var allPaths = requestContext.RouteData.Values[key] as string;
if (!string.IsNullOrEmpty(allPaths))
{
requestContext.RouteData.Values[key] = allPaths.Split('/');
}
return base.GetHttpHandler(requestContext);
}
}
Register the route handler.
注册路由处理程序。
routes.Add(new Route("tag/{*tags}",
new RouteValueDictionary(
new
{
controller = "Tag",
action = "Index",
}),
new AllPathRouteHandler("tags")));
Get the tags as a array in the controller.
在控制器中以数组的形式获取标记。
public ActionResult Index(string[] tags)
{
// do something with tags
return View();
}
#4
5
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 by global.asax
in the Application_Start()
method. So, in .NET 4.0, make sure you add your custom routes there. This article covers it beautifully.
为了防止任何人在。net 4.0中使用MVC时遇到这种情况,您需要小心地定义路由。我很高兴去环球旅行。asax和添加在这些答案(以及其他教程)中建议的路线,并没有得到任何结果。我的路由都默认为{controller}/{action}/{id}。向URL添加更多的段给了我一个404错误。然后我发现了RouteConfig。在App_Start文件夹中的cs文件。这个文件被全局调用。asax在Application_Start()方法中。所以,在。net 4.0中,一定要添加自定义路由。这篇文章写得很好。
#1
55
Like this:
是这样的:
routes.MapRoute("Name", "tag/{*tags}", new { controller = ..., action = ... });
ActionResult MyAction(string tags) {
foreach(string tag in tags.Split("/")) {
...
}
}
#2
26
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.
所有的捕获都将给你原始的字符串。如果您想要一种更优雅的方法来处理数据,您可以始终使用自定义路由处理程序。
public class AllPathRouteHandler : MvcRouteHandler
{
private readonly string key;
public AllPathRouteHandler(string key)
{
this.key = key;
}
protected override IHttpHandler GetHttpHandler(RequestContext requestContext)
{
var allPaths = requestContext.RouteData.Values[key] as string;
if (!string.IsNullOrEmpty(allPaths))
{
requestContext.RouteData.Values[key] = allPaths.Split('/');
}
return base.GetHttpHandler(requestContext);
}
}
Register the route handler.
注册路由处理程序。
routes.Add(new Route("tag/{*tags}",
new RouteValueDictionary(
new
{
controller = "Tag",
action = "Index",
}),
new AllPathRouteHandler("tags")));
Get the tags as a array in the controller.
在控制器中以数组的形式获取标记。
public ActionResult Index(string[] tags)
{
// do something with tags
return View();
}
#3
#4
5
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 by global.asax
in the Application_Start()
method. So, in .NET 4.0, make sure you add your custom routes there. This article covers it beautifully.
为了防止任何人在。net 4.0中使用MVC时遇到这种情况,您需要小心地定义路由。我很高兴去环球旅行。asax和添加在这些答案(以及其他教程)中建议的路线,并没有得到任何结果。我的路由都默认为{controller}/{action}/{id}。向URL添加更多的段给了我一个404错误。然后我发现了RouteConfig。在App_Start文件夹中的cs文件。这个文件被全局调用。asax在Application_Start()方法中。所以,在。net 4.0中,一定要添加自定义路由。这篇文章写得很好。