3

现在我有一个匹配的地图路线

foreach(var subcat in Sports.Categories)
{
routes.MapRoute("MultiRoute" + i, subcat+"/{title}", 
new { controller = "Routing", action = "Redirect", category = subcat });
}

我有一些运动类别:棒球、足球、篮球等,但我也想将篮球与 BasketBall-Plays、BasketBall-Highlights、Top-10-Basketball 等相匹配...

这意味着我必须允许 subcat 在之前和之后使用通配符,因为 BasketBall-Plays 包含单词 Basketball,我希望它重定向到那里。

如何为地图路由创建包含部分?

编辑:TLDR:我有一个名为篮球的子猫,但我想匹配每个包含篮球一词的网址。所以 Basketball-plays/top-10 实际上应该打篮球/top-10

试过:

foreach(var subcat in Sports.Categories)
    {
    routes.MapRoute("MultiRoute" + i, subcat+"/{title}", 
    new { controller = "Routing", action = "Redirect", category = subcat }),
new{sub = ".*"+sub+".*" });
    }
4

1 回答 1

5

如果我理解正确,以下应该有效,

var subCategories = new[] { "Soccer", "BasketBall", "Golf" };
foreach (var subcat in subCategories)
{
    routes.MapRoute(subcat, "{subcat}/{title}",
                        new {controller = "Routing", action = "Redirect", category = subcat},
                        new { subcat = @".*\b" + subcat + @"\b.*" });
}

它将路由

  • 篮球/前10名
  • 篮球比赛/前 10 名
  • 篮球比赛/前10名
  • 前 10 名篮球/sometitle

希望这可以帮助。

于 2013-07-06T18:19:26.910 回答