【问题标题】:Infinite URL Parameters for ASP.NET MVC RouteASP.NET MVC 路由的无限 URL 参数
【发布时间】:2011-11-22 20:36:32
【问题描述】:

我需要一个可以在我的 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<string, string> 或List<string>?

注意:

这个问题在 IMO 中没有得到很好的解释,但我尽我所能去适应它。 in. 随意调整它

【问题讨论】:

    标签: asp.net asp.net-mvc asp.net-mvc-3 asp.net-mvc-routing


    【解决方案1】:

    像这样:

    routes.MapRoute("Name", "tag/{*tags}", new { controller = ..., action = ... });
    
    ActionResult MyAction(string tags) {
        foreach(string tag in tags.Split("/")) {
            ...
        }
    }
    

    【讨论】:

    • 嗯,看起来很整洁。会试一试的。
    • 这里的{*tags}有什么作用?特别是,*。
    • 这是一个包罗万象的参数。 msdn.microsoft.com/en-us/library/…
    • 那么,我们可以像这样使用 ASP.NET MVC 框架上的所有通配符参数还是只使用 *?
    • @tugberk:您只能使用 * 并且它必须始终是 catch-all 参数的第一个字符。它不是任何形状或形式的通配符。这只是意味着此路由参数将捕获您 URL 中从那时起的所有内容。
    【解决方案2】:

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

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

    【讨论】:

      【解决方案3】:

      那叫catch-all:

      tag/{*tags}
      

      【讨论】:

        【解决方案4】:

        以防万一有人在 .NET 4.0 中使用 MVC,您需要小心在哪里定义您的路线。我很高兴去global.asax 并按照这些答案(和其他教程)中的建议添加路线,但一无所获。我的路线都默认为{controller}/{action}/{id}。向 URL 添加更多段给了我 404 错误。然后我在 App_Start 文件夹中发现了 RouteConfig.cs 文件。原来这个文件是由global.asax 在Application_Start() 方法中调用的。因此,在 .NET 4.0 中,请确保在其中添加自定义路由。 This article 封面精美。

        【讨论】:

          【解决方案5】:

          在 asp .net core 中,您可以在路由中使用 * 例如

          [HTTPGet({*id})]

          此代码可以多参数,或者在使用带斜线的发送字符串时使用它们来获取所有参数

          【讨论】:

            猜你喜欢
            • 2017-10-08
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2023-03-30
            • 2010-11-22
            • 2010-11-13
            相关资源
            最近更新 更多