Asp.net MVC3映射路由:在{id}之前添加前缀

时间:2011-07-24 06:10:27

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

我有一个像http://..../controller/details/abc_1234这样的路线处理网址。我想将以下网址路由到上一个网址:

http://..../controller/abc/1234

以下代码似乎不起作用?

routes.MapRoute(
    "abc",
    "{controller}/abc/{id}",
    new { controller = "controller", action = "Details", id = "abc_{id}" }
);

routes.MapRoute(
    "Default", 
    "{controller}/{action}/{id}", 
    new { controller = "controller", action = "Details", id = UrlParameter.Optional } 
);

2 个答案:

答案 0 :(得分:2)

    routes.MapRoute(
    "abc",
    "{controller}/{action}/{key}_{id}",
    new { controller = "controller" }
);

你的行动如下:

    public virtual ActionResult Details(string key, string id)
{
}

答案 1 :(得分:0)

Try this with a action called `abc`

routes.MapRoute(
        "abc",
        "{controller}/abc/{id}",
        new { controller = "controller", action = "abc", id = "abc_{id}" }
    );