代码之家  ›  专栏  ›  技术社区  ›  erikkallen

Asp.net MVC对多个路由使用同一控制器

  •  0
  • erikkallen  · 技术社区  · 16 年前

    我有这样一个简单的控制器(不,不是真的,但假设我有)

    public class SomethingController : Controller {
        public ActionResult Method1() {
            return View("Something1");
        }
    
        public ActionResult Method2() {
            return View("Something2");
        }
    }
    

    现在我想将此控制器用于两种不同的路由:

    public static void RegisterRoutes(RouteCollection routes) {
        routes.MapRoute("Route 1", "Route1/{action}", new { controller = "Something" });
        routes.MapRoute("Route 2", "Route2/{action}", new { controller = "Something" });
    }
    

    到现在为止,没什么特别的。然而,在我看来 Something1

    Html.ActionLink("Do Something", "Method2")
    

    这应该是 <a href="Route1/Method2"... <a href="Route2/Method2"...

    1 回复  |  直到 16 年前
        1
  •  3
  •   Craig Stuntz    16 年前

    使用 Html.RouteLink 而不是 Html.ActionLink . 它允许您指定路由名称。