2010-02-18 121 views
0

我有一个简单的控制器这样的(不,不是真的,但让我们说我这样做)Asp.net MVC相同的控制器为多个路由

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"...,这取决于哪条路线导致了显示的视图控制器。如何才能做到这一点?

回答

3

使用Html.RouteLink而不是Html.ActionLink。它让你指定路由名称。

+0

@Obalix:真的吗?这将如何工作?我认为它只会找到名为Route1Controller的控制器。你能详细说明吗? – erikkallen 2010-02-18 21:36:36