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

本文关键字:id 加前缀 添加 mvc net 映射 路由 Asp | 更新日期: 2023-09-27 18:03:24

我有一个路由处理url像http://..../controller/details/abc_1234。我想把下面的url也路由到前一个:

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

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

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

,你的动作看起来像:

    public virtual ActionResult Details(string key, string id)
{
}
Try this with a action called `abc`
routes.MapRoute(
        "abc",
        "{controller}/abc/{id}",
        new { controller = "controller", action = "abc", id = "abc_{id}" }
    );