简体   繁体   English

ASP.NET MVC 路由的无限 URL 参数

[英]Infinite URL Parameters for ASP.NET MVC Route

I need an implementation where I can get infinite parameters on my ASP.NET Controller.我需要一个可以在我的 ASP.NET 控制器上获得无限参数的实现。 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.如您所见,它会在example.com/tag/之后获得无限数量的标签,斜线将是这里的分隔符。

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> ?使用Dictionary<string, string>List<string>

NOTE :笔记 :

The question is not well explained IMO but I tried my best to fit it.这个问题没有很好地解释 IMO,但我尽力适应它。 in. Feel free to tweak it in. 随意调整它

Like this:像这样:

routes.MapRoute("Name", "tag/{*tags}", new { controller = ..., action = ... });

ActionResult MyAction(string tags) {
    foreach(string tag in tags.Split("/")) {
        ...
    }
}

The catch all will give you the raw string. catch all 会给你原始字符串。 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();
}

这就是所谓的包罗万象

tag/{*tags}

Just in case anyone is coming to this with MVC in .NET 4.0, you need to be careful where you define your routes.万一有人要来此与MVC在.NET 4.0中,你需要一个定义你的路由要小心。 I was happily going to global.asax and adding routes as suggested in these answers (and in other tutorials) and getting nowhere.我很高兴去global.asax并按照这些答案(和其他教程)中的建议添加路由,但一无所获。 My routes all just defaulted to {controller}/{action}/{id} .我的路由都默认为{controller}/{action}/{id} Adding further segments to the URL gave me a 404 error.向 URL 添加更多段给了我 404 错误。 Then I discovered the RouteConfig.cs file in the App_Start folder.然后我在 App_Start 文件夹中发现了 RouteConfig.cs 文件。 It turns out this file is called by global.asax in the Application_Start() method.原来这个文件是由global.asaxApplication_Start()方法中调用的。 So, in .NET 4.0, make sure you add your custom routes there.因此,在 .NET 4.0 中,请确保在那里添加自定义路由。 This article covers it beautifully. 这篇文章很好地介绍了它。

in asp .net core you can use * in routing for example例如,在 asp .net core 中,您可以在路由中使用 *

[HTTPGet({*id})] [HTTPGet({*id})]

this code can multi parameter or when using send string with slash use them to get all parameters此代码可以多参数或使用带斜杠的发送字符串时使用它们来获取所有参数

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM