asp.net mvc可选参数

时间:2009-11-10 07:26:30

标签: .net asp.net asp.net-mvc .net-3.5 url-routing

我在这里尝试将1个与asp.net mvc合并的2条路线

这些路线是:

  

http://www.example.com/e/1

  

http://www.example.com/e/1,name-of-the-stuff

目前在Global.asax.cs中将其设置为:

routes.MapRoute(
    "EventWithName",
    "e/{id},{name}",
    new { controller = "Event", action = "SingleEvent", id = "" },
    new { id = @"([\d]+)" }
);

routes.MapRoute(
    "SingleEvent",
    "e/{id}",
    new { controller = "Event", action = "SingleEvent", id = "" },
    new { id = @"([\d]+)" }
);

我尝试通过修改第一条路线来处理它:

routes.MapRoute(
    "EventWithName",
    "e/{id}{name}",
    new { controller = "Event", action = "SingleEvent", id = "" },
    new { id = @"([\d]+)", name = @"^,(.*)" }
);

它不起作用,因为我需要用斜杠或至少一个字符分隔2个参数。

我最有可能解决这个问题的方法是使用正则表达式,因为我只需要id部分。该名称仅是描述性的,用于SEO目的。基本上有一种方法可以使用([\d]+),(.*)类型的正则表达式和id = "$1"类似的正则表达式吗?

1 个答案:

答案 0 :(得分:3)

也许有帮助

routes.MapRoute(
    "Default",
    "{controller}/{action}/{id}/{*keywords}",
    new { controller = "Home", action = "Index", id = 0 },
    new { id = "(\\d+)" }
);