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

发布于 2024-12-06 08:33:16 字数 667 浏览 5 评论 0原文

我需要一个可以在 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;

}

有什么已知的方法可以实现这一点吗?如何从控制器获取值?使用 DictionaryList

注意:

在我看来,这个问题没有得到很好的解释,但我尽力了。 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

〆凄凉。 2024-12-13 08:33:16

像这样:

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

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

Like this:

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

ActionResult MyAction(string tags) {
    foreach(string tag in tags.Split("/")) {
        ...
    }
}
揽清风入怀 2024-12-13 08:33:16

catch all 将为您提供原始字符串。如果您想要一种更优雅的方式来处理数据,您始终可以使用自定义路由处理程序。

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);
    }
} 

注册路由处理程序。

routes.Add(new Route("tag/{*tags}",
        new RouteValueDictionary(
                new
                {
                    controller = "Tag",
                    action = "Index",
                }),
        new AllPathRouteHandler("tags")));

在控制器中以数组形式获取标签。

public ActionResult Index(string[] tags)
{
    // do something with tags
    return View();
}

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();
}
<逆流佳人身旁 2024-12-13 08:33:16

这称为catch-all

tag/{*tags}

That's called catch-all:

tag/{*tags}
┾廆蒐ゝ 2024-12-13 08:33:16

以防万一有人在 .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 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.

久夏青 2024-12-13 08:33:16

在 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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文